알고리즘/LeetCode

[LeetCode/easy/Array] Remove Duplicates from Sorted Array

녕이 2022. 8. 16. 15:57
728x90

 

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

 

Remove Duplicates from Sorted Array - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

감소하지 않는 순서로 정렬된 nums 배열이 주어진다. 각 유니크한 요소들이 오직 한 번만 나타나도록 중복을 삭제해라.

input array를 수정하는 방식으로 해야됨. 중복 수를 제거하고나서 몇 개가 나오는지 출력해라.

 

STL vector에서 earse/unique를 사용하면 간편하다.

꼭 잊지 말고 기억하긔~~

 

int removeDuplicates(vector<int>& nums) {
    nums.erase(unique(nums.begin(), nums.end()), nums.end());
    return nums.size();
}

 

 

 

 

 

 

 

728x90