-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnique Number of Occurrences
43 lines (37 loc) · 1.11 KB
/
Unique Number of Occurrences
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
bool uniqueOccurrences(vector<int>& arr) {
vector<int>ans;
// sort the given array
sort(arr.begin(),arr.end());
// counting the occurence
int count = 1;
for(int i=0; i<arr.size();i++){
if(i+1 < arr.size()){ // check element is in the arry
if(arr[i]==arr[i+1]){
count++;
}
else{
ans.push_back(count);
count = 1;
}
}
else{
ans.push_back(count);
}
}
// occurence are stored in ans vector , now sort the ans vector
sort(ans.begin(),ans.end());
//check the repetation of occurence
//if repetation is found --> return false
// else return true
for(int i =0; i<ans.size();i++){
if(i+1 < ans.size()){
if(ans[i] == ans[i+1]){
return false;
}
}
}
return true;
}
};