-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueNumOfOccurences.cpp
More file actions
40 lines (35 loc) · 864 Bytes
/
UniqueNumOfOccurences.cpp
File metadata and controls
40 lines (35 loc) · 864 Bytes
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
#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
// todo: Given an array of integers arr, write a function that returns true if and
// only if the number of occurrences of each value in the array is unique.
bool uniqueOccurrences(vector<int>& arr) {
if(arr.size() == 2 && arr[1] != arr[0])
return false;
set<int> s;
sort(arr.begin(), arr.end());
int i=1;
while(i < arr.size())
{
int sz = s.size();
int count = 1;
while(i < arr.size() && arr[i-1] == arr[i])
{
count++;
i++;
}
i++;
s.insert(count);
if(sz == s.size())
return false;
}
return true;
}
int main()
{
vector<int> v = {1,2,2,1,1,3};
cout << uniqueOccurrences(v);
}
// LC: Q.1207