천천히... leetcode most interview 어쩌구 뽀개기 시작
Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
입출력 예
Input: nums = [1,1,2]
Output: 2, nums = [1,2]
새로운 배열을 생성하지 않고 in place input은 배열이고 정렬되어 있는 상태이다. slice를 사용하는 방법 먼저 생각했다.
그러다가 slice는 배열을 복사하기 때문에 in place가 아니라는 생각이 들었고 splice를 사용하였다.
var removeDuplicates = function(nums) {
for(let i = nums.length - 1; i > 0; i--){
if(nums[i] === nums[i - 1]) nums.splice(i, 1);
}
return nums.length;
};