0

How to remove n duplicates in JavaScript Arrays

For example: n = 2 and the array is [1,1,1,1,2,2,3,4,4,5] The desired result: [1,1,2,2,3,4,4,5]

4th Feb 2024, 5:53 PM
Chloe Robinson
Chloe Robinson - avatar
3 Respuestas
+ 1
Attempts?
4th Feb 2024, 6:04 PM
A͢J
A͢J - avatar
+ 2
it is more accurate to say you are limiting the maximum allowed duplicates to n.
5th Feb 2024, 12:26 PM
Bob_Li
Bob_Li - avatar
+ 1
According to your example, you are not removing n duplicates. In the original array, 2 and 4 both appear twice, and in the expected result array, none of them are removed. Instead, you seem to be KEEPing max of n copies of each element. There could be several possible ways to solve this, but to understand what solution would benefit your current skill level and knowledge of Javascript, you should at least give it a try and explain your thought process.
5th Feb 2024, 5:39 AM
Tibor Santa
Tibor Santa - avatar