같은수는 2개까지는 배열에 저장하지만 3번째부터는 저장안한다.
가장 간단한 방법을 생각하고 싶었고 해당 수가 몇개있는지를 확인하기 위해서 굳이 map을 안써도 괜찮지만 제일 편리하게 작성할수 있을 것이라고 생각해서 Map()으로 작성하였습니다.
var removeDuplicates = function(nums) {
//의사코드
//Map()을 통해 배열에 겹치는 수가 1개 이상인지 확인할것
const map = new Map()
let index = 0
for(let i =0;i<nums.length;i++){
if(!map.has(nums[i])){
map.set(nums[i],1)
nums[index]=nums[i]
index ++
}else{
if(map.get(nums[i])==1){
map.set(nums[i],2)
nums[index]=nums[i]
index++
}
}
}
return index
};
'코테' 카테고리의 다른 글
LeetCode Top Interview 150 - 125. Valid Palindrome (0) | 2023.08.25 |
---|---|
LeetCode Top Interview 150 - 189. Rotate Array (0) | 2023.08.24 |
LeetCode Top Interview 150 - 121. Best Time to Buy and Sell Stock (0) | 2023.08.24 |
LeetCode Top Interview 150 - 26. Remove Duplicates from Sorted Array (0) | 2023.08.23 |
LeetCode Top Interview 150 - 80. Remove Duplicates from Sorted Array II (0) | 2023.08.23 |