Java Solution; O(n) running time, O(1) memory, 12ms.


  • 2
    S
    public class Solution {
      public int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
    
        int seen = nums[0];
        int indx = 1;
    
        for (int i = 1; i < nums.length; i++) {
          final int n = nums[i];
          if (n != seen) {
            nums[indx++] = n;
            seen = n;
          }
        }
    
        return indx;
      }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.