From 05aa05bb81b914f2d1ffba241c056534c3535d35 Mon Sep 17 00:00:00 2001 From: dmswl6310 Date: Wed, 26 Aug 2026 16:40:15 +0900 Subject: [PATCH] 0826 --- ...940. Limit Occurrences in Sorted Array.js" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "leetcode3/\355\231\251\354\235\200\354\247\200/3940. Limit Occurrences in Sorted Array.js" diff --git "a/leetcode3/\355\231\251\354\235\200\354\247\200/3940. Limit Occurrences in Sorted Array.js" "b/leetcode3/\355\231\251\354\235\200\354\247\200/3940. Limit Occurrences in Sorted Array.js" new file mode 100644 index 00000000..77bf900f --- /dev/null +++ "b/leetcode3/\355\231\251\354\235\200\354\247\200/3940. Limit Occurrences in Sorted Array.js" @@ -0,0 +1,26 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var limitOccurrences = function (nums, k) { + const count = Array(101).fill(0); + const result = []; + + const putNumber = function (num, repeat) { + for (let i = 0; i < repeat; i++) { + result.push(num); + } + }; + + for (let i = 0; i < nums.length; i++) { + const num = nums[i]; + count[num]++; + if (i === nums.length - 1 || num !== nums[i + 1]) { + if (count[num] <= k) putNumber(num, count[num]); + else putNumber(num, k); + } + } + + return result; +};