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]
3 Antworten
+ 1
Attempts?
+ 2
it is more accurate to say you are limiting the maximum allowed duplicates to n.
+ 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.