Title Description:
Given a sorted array, remove the duplicates in place such, all element appear only once and return the new L Ength.
Do the allocate extra space for another array, and you must does this on place with constant memory.
For example,
Given input array nums = [1,1,2]
,
Your function should return length = 2
, with the first of the elements of nums being and 1
2
Respectivel Y. It doesn ' t matter what are you leave beyond the new length.
Problem Solving Ideas:
Iterate over the group once and set a counter to add one when the elements are different before and after the array, and to place a different number at the corresponding counter.
The code is as follows:
public int removeduplicates (int[] nums) {int length = Nums.length;int count = 1;if (length = = 0) return 0;for (int i = 1; i < Nums.length; i++) {if (nums[i-1] = = Nums[i]) continue;else {Nums[count] = nums[i];count++;}} return count;}
Java [Leetcode 26]remove duplicates from Sorted Array