Back to all solutions

#1207 - Unique Number of Occurrences

Problem 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.

Solution

/**
 * @param {number[]} arr
 * @return {boolean}
 */
var uniqueOccurrences = function(arr) {
  const map = new Map();
  arr.forEach(n => map.set(n, map.has(n) ? map.get(n) + 1 : 1));
  const occurrences = Array.from(map.values());
  return new Set(occurrences).size === occurrences.length;
};