Skip to content

Commit 2624c95

Browse files
committed
Add solution #1338
1 parent 680de24 commit 2624c95

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,238 LeetCode solutions in JavaScript
1+
# 1,239 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1015,6 +1015,7 @@
10151015
1334|[Find the City With the Smallest Number of Neighbors at a Threshold Distance](./solutions/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.js)|Medium|
10161016
1335|[Minimum Difficulty of a Job Schedule](./solutions/1335-minimum-difficulty-of-a-job-schedule.js)|Hard|
10171017
1337|[The K Weakest Rows in a Matrix](./solutions/1337-the-k-weakest-rows-in-a-matrix.js)|Easy|
1018+
1338|[Reduce Array Size to The Half](./solutions/1338-reduce-array-size-to-the-half.js)|Medium|
10181019
1342|[Number of Steps to Reduce a Number to Zero](./solutions/1342-number-of-steps-to-reduce-a-number-to-zero.js)|Easy|
10191020
1351|[Count Negative Numbers in a Sorted Matrix](./solutions/1351-count-negative-numbers-in-a-sorted-matrix.js)|Easy|
10201021
1352|[Product of the Last K Numbers](./solutions/1352-product-of-the-last-k-numbers.js)|Medium|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1338. Reduce Array Size to The Half
3+
* https://leetcode.com/problems/reduce-array-size-to-the-half/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array arr. You can choose a set of integers and remove all the
7+
* occurrences of these integers in the array.
8+
*
9+
* Return the minimum size of the set so that at least half of the integers of the array
10+
* are removed.
11+
*/
12+
13+
/**
14+
* @param {number[]} arr
15+
* @return {number}
16+
*/
17+
var minSetSize = function(arr) {
18+
const map = new Map();
19+
const halfLength = arr.length / 2;
20+
21+
arr.forEach(num => map.set(num, (map.get(num) || 0) + 1));
22+
23+
const frequencies = Array.from(map.values()).sort((a, b) => b - a);
24+
25+
let removedCount = 0;
26+
let result = 0;
27+
28+
for (const freq of frequencies) {
29+
removedCount += freq;
30+
result++;
31+
if (removedCount >= halfLength) break;
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)