code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

026 Remove Duplciates From Sorted Array

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

function removeDuplciatesFromSortedArray(nums) {
  let j = 0
  for (let i = 1; i < nums.length; i++) {
    if(nums[i] === nums[i-1]){
      j++;
    } else {
      nums[i-j] = nums[i]
    }
  }
  console.log(nums)
  return nums.length - j
}

test('remove Duplciates From Sorted Array', () => {
  expect(removeDuplciatesFromSortedArray([1,1,2])).toEqual(2)
  expect(removeDuplciatesFromSortedArray([0,0,1,1,1,2,2,3,3,4])).toEqual(5)
});

Created 2020-03-31T14:01:50+00:00 · Edit