Answers for "given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. do not allocate extra space for another array, you must do this by modifying the input array in-place with o(1) extra memory."

3

remove duplicates from sorted array

// Java
public int removeDuplicates(int[] nums) {
    if (nums.length == 0) return 0;
    int i = 0;
    for (int j = 1; j < nums.length; j++) {
        if (nums[j] != nums[i]) {
            i++;
            nums[i] = nums[j];
        }
    }
    return i + 1;
}
Posted by: Guest on June-18-2020

Code answers related to "given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. do not allocate extra space for another array, you must do this by modifying the input array in-place with o(1) extra memory."

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language