-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathunique_occurences.cpp
36 lines (36 loc) · 983 Bytes
/
unique_occurences.cpp
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
/*
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.
Input: arr = [1,2,2,1,1,3]
Output: true
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
*/
#include<bits/stdc++.h>
using namespace std;
bool check_unique(vector<int>& V, int n){
map<int, int> M;
for(int x : V){
M[x]++;
}
map<int, int> second_map;
for(auto x : M){
if(second_map[x.second] > 0) // if same map value encountered then return false
return false;
else
second_map[x.second]++;
}
return true;
}
int main(){
int n;
cin >> n;
vector<int> V(n);
for(int i = 0; i < n; i++){
cin >> V[i];
}
if(check_unique(V, n))
cout << "True";
else
cout << "False";
return 0;
}