We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0caf8c6 commit 214077dCopy full SHA for 214077d
Leetcode/July LeetCoding Challenge 2021/1338. Reduce Array Size to The Half.js
@@ -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