Back to problems

Remove Duplicates from Sorted Array with At Most K Occurrences

Algorithm · Oracle · Medium

You are given an integer array nums that is already sorted in non-decreasing order, along with an integer k. Modify nums in place so that after the modification, every distinct value appears no more than k times. For each distinct value, keep its first k occurrences and discard any later occurrences. The retained values must remain in the same relative order as they appeared originally. Return the new length m. The first m positions of nums must contain the retained values;…

Checking your access…