Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prayagupa/724d6dfc003d2b6e9e27 to your computer and use it in GitHub Desktop.
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].
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