-
Notifications
You must be signed in to change notification settings - Fork 1
/
1207. Unique Number of Occurrences.java
56 lines (46 loc) · 1.14 KB
/
1207. Unique Number of Occurrences.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public boolean uniqueOccurrences(int[] arr) {
if(arr.length==2)
{
if(arr[0]==arr[1])
{
return true;
}
else
{
return false;
}
}
ArrayList<Integer> quan = new ArrayList<Integer>();
Arrays.sort(arr);
int count =1 ;
for(int i=0;i<arr.length-1;i++)
{
if(arr[i]==arr[i+1])
{
count = count+1;
}
else
{
if(quan.contains(count))
{
return false;
}
quan.add(count);
count = 1;
}
}
return true;
}
}
//OPTIMAL SOLUTION
class Solution {
public boolean uniqueOccurrences(int[] arr) {
Map<Integer, Integer> freq = new HashMap<>();
for (int num : arr) {
freq.put(num, freq.getOrDefault(num, 0) + 1);
}
Set<Integer> freqSet = new HashSet<>(freq.values());
return freq.size() == freqSet.size();
}
}