Forked from guolinaileen/Remove Duplicates from Sorted Array.java
Last active
September 9, 2015 05:32
-
-
Save prayagupa/724d6dfc003d2b6e9e27 to your computer and use it in GitHub Desktop.
Given a sorted array, 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 in place with constant memory.For example,Given input array A = [1,1,2],Your function should return length = 2, and A is now [1,2].
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
object Solution { | |
def removeDuplicates(A : Array[Int]) : Array[Int] = { | |
val length=A.length | |
if(length==0 || length==1) A | |
var index=1 | |
for(compareIndex <- 1 to length) { | |
if(A(compareIndex)!=A(compareIndex-1)) { | |
A(index)=A(compareIndex) | |
index=index+1 | |
} | |
} | |
if(index<length) A(index)='\0' | |
A | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment