Write a function to filter or modify an array in-place, such as removing elements that repeat more than twice.
Implement keep_at_most_two(nums). Preserve the order of retained values, overwrite the beginning of nums in place, and return the new logical length k. Only nums[:k] is part of the result; values beyond that prefix do not matter. The input may be unsorted and may contain negative or repeated integers.
nums.k, with each distinct value appearing no more than twice in nums[:k].def keep_at_most_two(nums):