본문 바로가기
Algorithm

[leetCode 26번] Remove Duplicates from Sorted Array (Java)

by ddahu 2023. 8. 24.

1. 문제

 

LeetCode - The World's Leading Online Programming Learning Platform

 

LeetCode - The World's Leading Online Programming Learning Platform

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

 

 


2. 문제 설명

 

간단하게 정렬된 배열에서 중복된 요소를 제거 하는 문제 이다.

 


 

3. 소스코드

class Solution {
    public int removeDuplicates(int[] nums) {
        HashSet<Integer> sets = new HashSet<>();

        int idx = 0; 
        
        for (int num : nums) {
            if (sets.add(num)) {
                nums[idx] = num;
                idx++;
            }
        }
        
        return idx;
    }
}

 


4.  문제 해설

 

그 이전에 풀었던 27번 문제 에 HashSet구조를 넣어서 풀었다 HashSet은 중복된 요소를 허용하지 않기 때문에 HashSet에 요소를 추가하면서 삭제를 해주면 된다.

[leetCode 27번] Remove Element (JAVA) (tistory.com)

 

[leetCode 27번] Remove Element (JAVA)

1.문제 LeetCode - The World's Leading Online Programming Learning Platform LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get pre

ldh7728.tistory.com