Skip to content

Commit 3a116fb

Browse files
committed
Runtime: 54 ms (Top 59.88%) | Memory: 43.70 MB (Top 21.19%)
1 parent 2d9ddc8 commit 3a116fb

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
// Runtime: 54 ms (Top 59.88%) | Memory: 43.70 MB (Top 21.19%)
2+
3+
// time O(n) space O(n)
14
var uniqueOccurrences = function(arr) {
2-
const obj = {};
3-
// Creating hashmap to store count of each number
4-
arr.forEach(val => obj[val] = (obj[val] || 0) + 1);
5-
// Creating an array of the count times
6-
const val = Object.values(obj).sort((a, b) => a-b);
7-
// Now, just finding the duplicates
8-
for(let i = 0; i<val.length-1; i++){
9-
if(val[i]===val[i+1]) return false;
5+
const map = {}
6+
7+
for(const number of arr) {
8+
if(map[number]) {
9+
map[number] += 1
10+
} else {
11+
map[number] = 1
12+
}
1013
}
11-
return true;
14+
15+
const frequencies = Object.values(map)
16+
const set = new Set(frequencies)
17+
18+
return frequencies.length === set.size
1219
};

0 commit comments

Comments
 (0)