Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1597 from aadil42/patch-38
Browse files Browse the repository at this point in the history
  • Loading branch information
aakhtar3 authored Dec 26, 2022
2 parents e1c63df + 337e694 commit b938de5
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions javascript/26-Remove-Duplicates-from-Sorted-Array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Linear
* Time O(N) | Space O(1)
* https://leetcode.com/problems/remove-duplicates-from-sorted-array/
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = (nums) => {
let [left, right] = [0, 0];

while (right < nums.length) {
const [leftVal, rightVal] = [nums[left], nums[right]];

const isEqual = (rightVal === leftVal);
if (!isEqual) {
left++;
nums[left] = rightVal;
}

right++;
}

return (left + 1);
};

0 comments on commit b938de5

Please sign in to comment.