Created
July 17, 2016 05:18
-
-
Save jianminchen/f7156b033b783b3a01c2ad2cb3d1b313 to your computer and use it in GitHub Desktop.
Remove duplicate from sorted array - facebook code lab - pass all test cases.
This file contains 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
public class Solution { | |
public int removeDuplicates(List<Integer> a) { | |
if(a==null || a.size() == 0) | |
return 0; | |
int prev = Integer.valueOf(a.get(0)); | |
int index = 0; | |
int count = 0; | |
for(int i=1; i< a.size(); i++) | |
{ | |
int curr = Integer.valueOf(a.get(i)); | |
if(curr != prev) | |
{ | |
count++; | |
a.set(count, curr); // set array new value | |
prev = curr; | |
} | |
} | |
return count +1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment