Skip to content

Latest commit

 

History

History
24 lines (21 loc) · 558 Bytes

2022-11-30.md

File metadata and controls

24 lines (21 loc) · 558 Bytes

题目

  1. Unique Number of Occurrences

Given an array of integers arr, return true if the number of occurrences of each value in the array is unique, or false otherwise.

思路

代码

var uniqueOccurrences = function(arr) {
  const num2count = {}
  for (let i = 0; i < arr.length; i++) {
    num2count[arr[i]] = (num2count[arr[i]] || 0) + 1
  }
  const count2num = {}
  for (const num in num2count) {
    if (num2count[num] in count2num) {
      return false
    }
    count2num[num2count[num]] = num
  }
  return true
};