Skip to content

Commit 44aaf6a

Browse files
authored
Create 1338-reduce-array-size-to-the-half.js
1 parent db31e08 commit 44aaf6a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

1338-reduce-array-size-to-the-half.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} arr
3+
* @return {number}
4+
*/
5+
var minSetSize = function(arr) {
6+
const hash = {}
7+
for(const e of arr) {
8+
if(hash[e] == null) hash[e] = 0
9+
hash[e]++
10+
}
11+
const n = arr.length
12+
const entries = Object.entries(hash)
13+
entries.sort((a, b) => b[1] - a[1])
14+
let res= 0
15+
let cnt = 0
16+
for(const [k, v] of entries) {
17+
cnt += v
18+
res++
19+
if(cnt >= n / 2) break
20+
}
21+
22+
return res
23+
};

0 commit comments

Comments
 (0)