[Leetcode] Remove Duplicates from Sorted Array - 자바스크립트, JavaScript

Jin·2023년 2월 23일

Algorithm

목록 보기
5/13

문제

풀이

var removeDuplicates1 = function (nums) {
  for (let i = 0; i < nums.length; i++) {
    const num = nums[i];
    const nextNum = nums[i + 1];
    if (num === nextNum) {
      nums.splice((i + 1), 1);
      i--;
    }
  }
  return nums.length;
};

var removeDuplicates2 = function (nums) {
  const set = new Set(nums);
  nums.splice(0, nums.length, ...set);
  return nums.length;
};
profile
Nothing changes if nothing changes

0개의 댓글