Skip to content

Instantly share code, notes, and snippets.

@osahondev
Last active April 12, 2021 07:51
Show Gist options
  • Select an option

  • Save osahondev/d242e74f8d87edebe4b4b017721131ef to your computer and use it in GitHub Desktop.

Select an option

Save osahondev/d242e74f8d87edebe4b4b017721131ef to your computer and use it in GitHub Desktop.
Algorithm to remove duplicates from a sorted array
const removeDuplicates = (nums) =>{
if( nums == undefined )
return 0;
if( nums.length == 0 )
return 0;
let pointerOne = 0;
let pointerTwo = 1;
let occurence = 0;
while( pointerOne < nums.length ){
if( nums[pointerOne] != nums[pointerTwo] ){
occurence++;
pointerOne = pointerTwo;
}
pointerTwo++;
}
return occurence;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment