본문 바로가기
Problem Solving/LeetCode

[LeetCode] 27. Remove Element - Java

by graycode 2026. 5. 15.

 문제 링크

 

Remove Element - LeetCode

Can you solve this real interview question? Remove Element - Given an integer array nums and an integer val, remove all occurrences of val in nums in-place [https://en.wikipedia.org/wiki/In-place_algorithm]. The order of the elements may be changed. Then r

leetcode.com

 

 풀이 코드

public class Solution {

    public int removeElement(int[] nums, int val) {
        int idx = 0;
        for (int i = 0; i < nums.length; i++) if (nums[i] != val) nums[idx++] = nums[i];

        return idx;
    }

}

댓글