Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Use a slow pointer starting at index 2. The fast pointer scans from index 2 onward. Compare nums[fast] with nums[slow - 2]: if they differ, the element is safe to keep (at most 2 copies), so copy it to slow and advance slow.
Comparing with nums[slow - 2] ensures at most two copies of any value survive. If nums[fast] equals nums[slow - 2], there are already two copies in the valid region, so it must be skipped.
Updated Feb 2026