0000 Find out the unique elements from the Array | MyCareerwise

FOR FREE YEAR SOLVED

Find out the unique elements from the Array

Back to Programming

Description

The elements of the array are taken as input. The program is to find out the unique elements from the array. Unique elements refer to those elements which appear exactly ones in the array.

Algorithm

INPUT: The array elements
OUTPUT: the unique elements of the array
PROCESS:
Step 1: [Taking inputs from the user]
	Read n [number of elements]
	For i = 0 to n-1 repeat
		Read a[i]
	[End of ‘for’ loop]
Step 2: [Finding the unique elements]
For i=0 to n-1 repeat
        		Set c<-0
	    	For j=0 to i-1 repeat
            			If a[i]=a[j] then
                			Set c<-c+1
            			[End of ‘if’]
        		[End of ‘for’ loop]
       		For k=i+1 to n-1 repeat
            			If a[i]=a[k] then
                			Set c<-c+1
            			[End of ‘if’]
        		[End of ‘for’ loop]
		If c=0 then
          			Print a[i]
		[End of ‘if’]
        [End of ‘for’ loop]

Code

Complexity:

The time complexity to take the number of elements is O(n) and the time complexity to find the unique element is also O(n2).

Therefore, the time complexity of this program is O(n2).