easyArraysPattern: Hashing
Count Unique Elements Solution
Problem Statement
You are given an array of integers `nums`. Write a function to count the number of unique elements in the array.
Examples
Example 1:
Input:[1, 2, 3, 2, 1]
Output:3
Explanation: There are 3 unique identifiers: 1, 2, and 3
Example 2:
Input:[4, 4, 4, 4]
Output:1
Explanation: There is only 1 unique identifier: 4
Example 3:
Input:[]
Output:0
Explanation: There are no unique identifiers in an empty array
Constraints
- The array will not be null.
- The array will have at least two elements.
- Duplicates may appear multiple times.
Time: O(n) Space: O(n)
Use a set data structure to store unique identifiers with a time complexity of O(n)
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
