본문 바로가기
Problem Solving/LeetCode

[LeetCode] 26. Remove Duplicates from Sorted Array - Java

by graycode 2026. 5. 14.

 문제 링크

 

Remove Duplicates from Sorted Array - LeetCode

Can you solve this real interview question? Remove Duplicates from Sorted Array - Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place [https://en.wikipedia.org/wiki/In-place_algorithm] such that each unique element ap

leetcode.com

 

 풀이 코드

public class Solution {

    public int removeDuplicates(int[] nums) {
        if (nums.length == 1) return 1;

        int idx = 1;
        for (int i = 1; i < nums.length; i++) if (nums[i - 1] != nums[i]) nums[idx++] = nums[i];

        return idx;
    }

}

댓글