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 to track the position for the next unique element and a fast pointer to scan through the array. When the fast pointer finds a value different from nums[slow], increment slow and copy the new value there.
In a sorted array, duplicates are adjacent. The slow pointer only advances when a new unique value is found, naturally compacting unique elements to the front.
Updated Feb 2026