Notice
Recent Posts
Recent Comments
Link
.Zzumbong
[leetCode/JS] 1207. Unique Number of Occurrences ๋ณธ๋ฌธ
๋์ด๋ [ ๐ ] 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%๊น์ง ์ฌ๋ ธ๋ค.
'coding test > leetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetCode/JS] 1657. Determine if Two Strings Are Close (0) | 2022.12.02 |
---|---|
[leetCode/JS] 1704. Determine if String Halves Are Alike (0) | 2022.12.01 |
[leetCode/JS] 380. Insert Delete GetRandom O(1) (0) | 2022.11.29 |
[leetCode/JS] 2225. Find Players With Zero or One Losses (0) | 2022.11.28 |
[leetCode/JS] 446. Arithmetic Slices II - Subsequence (0) | 2022.11.27 |
0 Comments