The array is sorted, and each value may appear at most twice. Any third (or later) occurrence of a value must be removed. The constraint is doing this in-place: we cannot allocate a second array, and we return the count of valid elements left in the front of nums.
Because the array is sorted, all copies of a value are adjacent. We do not need a hash map to count frequencies, since consecutive equal elements already group the duplicates together. The question is how to decide which elements to keep and which to skip while overwriting the array in a single pass.
One element-level rule covers it: for any element at position i, if it equals the element two positions back in the result we are building, then it would be a third copy and must be skipped. If it differs from the element two positions back, it is safe to keep.
1 <= nums.length <= 3 * 10^4 → n reaches 30,000, so an O(n^2) double loop would run around 900 million comparisons. A single O(n) pass is the target.-10^4 <= nums[i] <= 10^4 → Values fit in a 32-bit int with no overflow concern.nums is sorted in non-decreasing order → Equal values are adjacent, so a single write pointer can decide what to keep without any auxiliary counting structure.Walk through the array while tracking how many times the current value has appeared in a row, and write an element to the result only when that running count is 2 or less. Because the array is sorted, maintaining the count needs no extra structure: every time the value changes from the previous one, reset the count to 1; otherwise increment it.
The count variable makes the rule explicit, since at every step it holds exactly how many copies of the current value have been seen so far.
writePos = 1 (the first element is always kept) and count = 1 (we've seen the first element once).1 to n - 1.nums[i] == nums[i - 1], increment count. Otherwise, reset count = 1.count <= 2, write nums[i] to nums[writePos] and increment writePos.writePos.writePos, count). No extra data structures.This works in one pass, but it carries a counter and the reset logic that goes with it. The next approach removes both by deriving the same decision from the result already written.
Instead of counting, compare the current element to the element two positions back in the result being built. If nums[i] != nums[writePos - 2], the element is safe to keep. If they are equal, the result already holds two copies of that value and this one would be a third.
The first two elements are always safe, since two elements can never form a third duplicate. So writePos starts at 2 and the scan starts at index 2, with no counter and no separate handling for the leading elements.
The result is built in-place at nums[0..writePos-1], and because the input is sorted and order is preserved, that prefix stays sorted too. Equal values in a sorted array are consecutive, so if nums[writePos - 2] equals some value v, then nums[writePos - 1] also equals v. The check nums[writePos - 2] == nums[i] therefore means the result already contains at least two copies of v, and skipping nums[i] keeps the count at two. The condition reads from the result rather than the original array, which is what removes the need for a separate counter.
writePos = 2 (first two elements are always kept).2 to n - 1.nums[i] != nums[writePos - 2], write nums[i] to nums[writePos] and increment writePos.writePos.writePos). The result is built in-place within the original array.