Skip to content

Commit c868be3

Browse files
Merge pull request #1914 from CodingTestStudy2/황은지
[황은지] Day28
2 parents fc298e1 + ab77c93 commit c868be3

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
var limitOccurrences = function (nums, k) {
7+
const count = Array(101).fill(0);
8+
const result = [];
9+
10+
const putNumber = function (num, repeat) {
11+
for (let i = 0; i < repeat; i++) {
12+
result.push(num);
13+
}
14+
};
15+
16+
for (let i = 0; i < nums.length; i++) {
17+
const num = nums[i];
18+
count[num]++;
19+
if (i === nums.length - 1 || num !== nums[i + 1]) {
20+
if (count[num] <= k) putNumber(num, count[num]);
21+
else putNumber(num, k);
22+
}
23+
}
24+
25+
return result;
26+
};

0 commit comments

Comments
 (0)