Array ( [0] => [1] => questions [2] => Strings [3] => Count-Unique-Elements )
Given list of elements. You have to find the number of unique elements in a given list.
First line contains a number N. Next line contains N integers separated by spaces.
Print number of unique elements present.
5 3 5 4 4 1
4
8 2 5 4 3 4 1 2 7
6
10 9 3 8 4 7 5 9 2 3 8
7
Login to see Discussion
Approach 1: Using Brute Force
Step-i) Initialize count to 0
Step-ii) check whether the current character as repeated in the string or not using loop
Step-iii) if it is repeated increment the count
Step-iv) Repeat ii and iii till all the characters of checked
Step-v) return count
Time Complexity: O(n^2)
Space Complexity: O(n)
Approach 2: Using set
Step-i)Create a empty set
Step-ii) Traverse the array and add elements to the set
Step-iii) Repeat the step ii till complete array is traversed
Step-iv) Find the size of the set
Step-v) Return the size of the set
Time Complexity: O(n)
Space Complexity: O(n)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution