Array ( [0] => [1] => questions [2] => Arrays [3] => Count-Unique-Elements ) Arrays | Count Unique Elements | THE INQUISITIVE





Count Unique Elements

LEVEL:Beginner

Description

Given list of elements. You have to find the number of unique elements in a given list.

Input Format

First line contains a number N. Next line contains N integers separated by spaces.

Output Format

Print number of unique elements present.


Example 1:

Input
5
3 5 4 4 1
Output
4
Example 2:

Input
8
2 5 4 3 4 1 2 7
Output
6
Example 3:

Input
10
9 3 8 4 7 5 9 2 3 8
Output
7

oops

Login to see Discussion




Approach

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.

oops

Login to see Solution