1 ms Java solution with 3 lines of code

  • 6
        public int removeElement(int[] nums, int val) {
            int index = 0;
            for(int i : nums) if(i != val) nums[index++] = i;
            return index;

    I saw a lot of really too complicated solutions.. don't complicate things.

  • 0

    Can you explain this please? Are you removing the mentioned 'val'?

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.