.Zzumbong

[leetCode/JS] 1207. Unique Number of Occurrences ๋ณธ๋ฌธ

coding test/leetCode

[leetCode/JS] 1207. Unique Number of Occurrences

์ญ˜๋ด‰ 2022. 11. 30. 14:05

๋‚œ์ด๋„ [ ๐Ÿ˜Š ] Easy

 

๋ฌธ์ œ ์„ค๋ช…

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

 

int ๋กœ ์ด๋ฃจ์–ด์ง„ arr๊ฐ€ ์žˆ๋‹ค. ์—ฌ๊ธฐ์— ๋‚˜์˜ค๋Š” ์ˆซ์ž๊ฐ€ ๋ฐœ์ƒํšŸ์ˆ˜๊ฐ€ ์œ ๋‹ˆํฌ ํ• ๋•Œ true์ด๋‹ค.

 

 

์ž…์ถœ๋ ฅ ์˜ˆ

Example 1:

Input: arr = [1,2,2,1,1,3]
Output: true
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1.
 No two values have the same number of occurrences.
1์€ 3๋ฒˆ, 2๋Š” 2๋ฒˆ, 3์€ 1๋ฒˆ ๋‚˜์™”๋‹ค. ๊ฐ ์ˆซ์ž์˜ ๋ฐœ์ƒํšŸ์ˆ˜๊ฐ€ 1,2,3 ๋ฒˆ์œผ๋กœ ์ค‘๋ณต๋˜์ง€ ์•Š์œผ๋‹ˆ True์ด๋‹ค.

 

Example 2:

Input: arr = [1,2]
Output: false

 

Example 3:

Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
Output: true

 

Constraints

  • 1 <= arr.length <= 1000
  • 1000 <= arr[i] <= 1000

 


 

๋‚ด ์†”๋ฃจ์…˜

  • Object๋กœ ๊ฐ ์ˆซ์ž๋ฅผ key๋กœ ๋‘๊ณ  ๋ฐœํ–‰ ํšŸ์ˆ˜๋ฅผ value๋กœ map์„ ์…‹ํŒ…ํ•œ๋‹ค.
  • Set์— ๋„ฃ์–ด์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ size์™€ sort์˜ length ๋น„๊ตํ•˜์—ฌ ์ค‘๋ณต์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ์ฒดํฌํ•œ๋‹ค.

 

/**
 * @param {number[]} arr
 * @return {boolean}
 */
var uniqueOccurrences = function(arr) {
  const map = {}
  for(const a of arr){
    map[a] = (map[a] || 0) + 1;
  }
  const sort = Object.values(map)
  return new Set(sort).size === sort.length
};

 

๊ฐ์ƒํ‰

  • ๊ต‰์žฅํžˆ ์‹ฌํ”Œ! ์ฒจ์—” ์ข€ ๋Š๋ ธ๋Š”๋ฐ ์ตœ์ ํ™” ๊ณ„์†ํ•ด์„œ 96%๊นŒ์ง€ ์˜ฌ๋ ธ๋‹ค.
0 Comments
๋Œ“๊ธ€์“ฐ๊ธฐ ํผ