1207. Unique Number of Occurrences

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
bool uniqueOccurrences(vector<int>& arr) {
unordered_map<int, int> mp;
unordered_set<int> s;
for(int a:arr) mp[a]++;
for(auto it = mp.begin() ; it!=mp.end() ; ++it)
{
if(s.count(it->second)) return false;
s.insert(it->second);
}
return true;

}
};

analysis

  • time complexity O(n)
  • space complexity O(n)