code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

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