|
| 1 | +# 1207. Unique Number of Occurrences |
| 2 | + |
| 3 | +- Difficulty: Easy. |
| 4 | +- Related Topics: Array, Hash Table. |
| 5 | +- Similar Questions: . |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +Given an array of integers `arr`, return `true` **if the number of occurrences of each value in the array is **unique** or **`false`** otherwise**. |
| 10 | + |
| 11 | + |
| 12 | +Example 1: |
| 13 | + |
| 14 | +``` |
| 15 | +Input: arr = [1,2,2,1,1,3] |
| 16 | +Output: true |
| 17 | +Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences. |
| 18 | +``` |
| 19 | + |
| 20 | +Example 2: |
| 21 | + |
| 22 | +``` |
| 23 | +Input: arr = [1,2] |
| 24 | +Output: false |
| 25 | +``` |
| 26 | + |
| 27 | +Example 3: |
| 28 | + |
| 29 | +``` |
| 30 | +Input: arr = [-3,0,1,-3,1,1,1,-3,10,0] |
| 31 | +Output: true |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +**Constraints:** |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +- `1 <= arr.length <= 1000` |
| 40 | + |
| 41 | +- `-1000 <= arr[i] <= 1000` |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +## Solution |
| 46 | + |
| 47 | +```javascript |
| 48 | +/** |
| 49 | + * @param {number[]} arr |
| 50 | + * @return {boolean} |
| 51 | + */ |
| 52 | +var uniqueOccurrences = function(arr) { |
| 53 | + var numMap = {}; |
| 54 | + for (var i = 0; i < arr.length; i++) { |
| 55 | + numMap[arr[i]] = (numMap[arr[i]] || 0) + 1; |
| 56 | + } |
| 57 | + var occureMap = {}; |
| 58 | + var nums = Object.keys(numMap); |
| 59 | + for (var j = 0; j < nums.length; j++) { |
| 60 | + if (occureMap[numMap[nums[j]]]) return false; |
| 61 | + occureMap[numMap[nums[j]]] = true; |
| 62 | + } |
| 63 | + return true; |
| 64 | +}; |
| 65 | +``` |
| 66 | + |
| 67 | +**Explain:** |
| 68 | + |
| 69 | +nope. |
| 70 | + |
| 71 | +**Complexity:** |
| 72 | + |
| 73 | +* Time complexity : O(n). |
| 74 | +* Space complexity : O(n). |
0 commit comments