Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 214077d

Browse files
1338. Reduce Array Size to The Half
1 parent 0caf8c6 commit 214077d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* https://leetcode.com/problems/reduce-array-size-to-the-half/
3+
* @param {number[]} arr
4+
* @return {number}
5+
*/
6+
var minSetSize = function(arr) {
7+
let ans = 0
8+
let count = {}
9+
arr.forEach((item) => {
10+
count[item] = (count[item] || 0) + 1
11+
})
12+
count = Object.keys(count).reduce((acc, item) => {
13+
acc.push(count[item])
14+
return acc
15+
}, [])
16+
count.sort((a, b) => b - a)
17+
let sum = arr.length / 2
18+
for (let i = 0; i < count.length && sum > 0; i++) {
19+
sum -= count[i]
20+
ans++
21+
}
22+
return ans
23+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /