Skip to content

Commit 76cff1a

Browse files
authored
Merge pull request #3589 from somilyadav7/main
Added Leetcode Solution 1207
2 parents e09e9da + 1d274d3 commit 76cff1a

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
id: unique-number-of-occurrences
3+
title: Unique Number of Occurrences
4+
sidebar_label: 1207. Unique Number of Occurrences
5+
tags:
6+
- Array
7+
- Hash Table
8+
description: "Solution to Leetcode 1207. Unique Number of Occurrences"
9+
---
10+
11+
## Problem Description
12+
13+
Given an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise.
14+
15+
### Examples
16+
17+
**Example 1:**
18+
19+
```
20+
Input: arr = [1,2,2,1,1,3]
21+
Output: true
22+
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: arr = [1,2]
29+
Output: false
30+
```
31+
32+
33+
34+
### Constraints
35+
- `1 <= arr.length <= 1000`
36+
- `-1000 <= arr[i] <= 1000`
37+
38+
### Approach
39+
1. Sort the input array arr to group identical elements together.
40+
2. Traverse the sorted array, counting occurrences of each element.
41+
3. Store the counts in a separate vector v.
42+
4. Sort the vector v to make it easier to check for duplicates.
43+
5. Iterate through v and check if adjacent elements are equal. If so, return false.
44+
6. If the loop completes, it means all counts are unique, and the function returns true.
45+
46+
### Complexity
47+
48+
Time complexity: $O(n)$
49+
Space complexity: $O(n)$
50+
51+
### Solution
52+
53+
#### Code in Different Languages
54+
55+
#### C++
56+
57+
```cpp
58+
class Solution {
59+
public:
60+
bool uniqueOccurrences(vector<int>& arr) {
61+
unordered_map<int,int>freq;
62+
for(auto x: arr){
63+
freq[x]++;
64+
}
65+
unordered_set<int>s;
66+
for(auto x: freq){
67+
s.insert(x.second);
68+
}
69+
return freq.size()==s.size();
70+
}
71+
};
72+
```
73+
74+
#### JAVA
75+
76+
```JAVA
77+
class Solution {
78+
public boolean uniqueOccurrences(int[] arr) {
79+
Map<Integer, Integer> freq = new HashMap<>();
80+
for (int x : arr) {
81+
freq.put(x, freq.getOrDefault(x, 0) + 1);
82+
}
83+
84+
Set<Integer> s = new HashSet<>();
85+
for (int x : freq.values()) {
86+
s.add(x);
87+
}
88+
89+
return freq.size() == s.size();
90+
}
91+
}
92+
```
93+
94+
#### PYTHON
95+
96+
```python
97+
class Solution:
98+
def uniqueOccurrences(self, arr: List[int]) -> bool:
99+
freq = {}
100+
for x in arr:
101+
freq[x] = freq.get(x, 0) + 1
102+
103+
return len(freq) == len(set(freq.values()))
104+
```
105+
106+
107+
108+
### Complexity Analysis
109+
110+
- Time Complexity: $O(n)$
111+
112+
- Space Complexity: $O(n)$
113+
114+
### References
115+
116+
- **LeetCode Problem**: Unique Number of Occurrences

0 commit comments

Comments
 (0)