A very simple Java solution with 5 lines O(n)&O(1)


  • 2
    F

    public class Solution {
    public int removeDuplicates(int[] nums) {
    int j = 1;
    for(int i = 0; i < nums.length-1; i++){
    if(nums[i] != nums[i+1]){
    nums[j++] = nums[i+1];
    }
    }
    return j;
    }
    }


  • 0
    A

    @FF_Ti Oh no! Can you format your code?


  • 0
    A

    Oh, Can you format ur code?


  • 1
    F

    lol, I don't know how to format code in that time.

    public class Solution {
        public int removeDuplicates(int[] nums) {
            int j = 1;
            for(int i = 0; i < nums.length - 1; i++){
                  if(nums[i] != nums[i + 1]){
                        nums[j++] = nums[i + 1];
                  }
            }
            return j;
        }
    }
    

Log in to reply
 

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