Created
August 13, 2017 23:48
-
-
Save sdpatil/98593e7af82049caef9e4e8284ecb0e2 to your computer and use it in GitHub Desktop.
Leetcode 80 Remove Duplicates from Sorted Array I
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Problem: Given an array of number remove {1,1,1,2,2,3} 3rd duplicate. | |
Ex. output in this case is {1,1,2,2,3,0} | |
Solution:- Basic idea is same as that of the RemoveDuplicate, you maintain write | |
index and start iterating through the array, whenver current element is more than | |
2nd last element in the new array then copy it to new array | |
*/ | |
public class RemoveDuplicatesII80 { | |
public int removeDuplicates(int[] nums) { | |
int i = 0; | |
for (int n : nums) | |
if (i < 2 || n > nums[i-2]) | |
nums[i++] = n; | |
return i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment