Skip to content

Commit 1de98dc

Browse files
committed
Add: Exercise 1207. Unique Number of Occurrences
1 parent b84bc48 commit 1de98dc

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 1207. Unique Number of Occurrences
2+
3+
Given an array of integers `arr`, return `true` if the number of occurrences of each value in the
4+
array is unique or `false` otherwise.
5+
6+
## Examples
7+
8+
### Example 1:
9+
10+
Input: `arr` = [1,2,2,1,1,3]
11+
Output: `true`
12+
Explanation: The value 1 has 3 occurrences, 2 has 2, and 3 has 1. No two values have the same number
13+
of occurrences.
14+
15+
### Example 2:
16+
17+
Input: `arr` = [1,2]
18+
Output: `false`
19+
20+
### Example 3:
21+
22+
Input: `arr` = [-3,0,1,-3,1,1,1,-3,10,0]
23+
Output: `true`
24+
25+
## Constraints
26+
27+
- 1 <= `arr.length` <= 1000
28+
- -1000 <= `arr[i]` <= 1000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} arr
3+
* @return {boolean}
4+
*/
5+
var uniqueOccurrences = function(arr) {
6+
const numMap = {}
7+
const freqMap = {}
8+
9+
for (let n of arr) {
10+
numMap.hasOwnProperty(n) ? numMap[n]++ : numMap[n] = 1
11+
}
12+
13+
for (let k in numMap) {
14+
const freq = numMap[k]
15+
freqMap.hasOwnProperty(freq) ? freqMap[freq]++ : freqMap[freq] = 1
16+
if (freqMap[freq] > 1) {
17+
return false
18+
}
19+
}
20+
21+
return true
22+
};
23+
24+
var output = uniqueOccurrences([1,2])
25+
26+
module.exports = uniqueOccurrences
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "1207-unique-numbers-of-occurrences",
3+
"version": "1.0.0",
4+
"description": "Given an array of integers `arr`, return `true` if the number of occurrences of each value in the array is unique or `false` otherwise.",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js",
9+
"test": "node test.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const assert = require('assert')
2+
const uniqueOccurrences = require('./index')
3+
4+
const tests = [
5+
{
6+
arr: [1,2,2,1,1,3],
7+
output: true,
8+
},
9+
{
10+
arr: [1,2],
11+
output: false,
12+
},
13+
{
14+
arr: [-3,0,1,-3,1,1,1,-3,10,0],
15+
output: true,
16+
},
17+
]
18+
19+
for (const t of tests) {
20+
assert.strict.deepEqual(uniqueOccurrences(t.arr), t.output)
21+
}

0 commit comments

Comments
 (0)