Algorithm 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.
algorithm Analysis: an ordered array, out of which the number of occurrences more than 1, return the number of remaining elements
code: set two pointers,I traverse the original array,J is the subscript after removing duplicates
Public intRemoveDuplicates (int[] nums) { if(Nums = =NULL|| Nums.length = = 0) return0; intLen =nums.length; intj = 1;//The first element of the array does not need to change, so the length is at least 1 for(inti = 1; i < Len; i++) {//array traversal starts from subscript 1 if(Nums[i] = = Nums[i-1]) Continue; Else{nums[j++] =Nums[i]; } } returnJ; }
Remove duplicates from Sorted Array leetcode java