Remove Element

Easy
253.

Remove Element

Remove Element is an easy coding problem on arrays and two pointers, asked in Amazon, Microsoft, TCS and Infosys interviews.

Given an integer array nums and an integer value, remove every occurrence of value from nums and return the remaining elements, keeping their original relative order.

Example 1

Input
nums = [3,2,2,3], value = 3
Output
[2,2]

Example 2

Input
nums = [0,1,2,2,3,0,4,2], value = 2
Output
[0,1,3,0,4]

Example 3

Input
nums = [1], value = 1
Output
[]

Constraints

  • 0 <= nums.length <= 40
  • 0 <= nums[i] <= 50
  • 0 <= value <= 50

Follow-up: The interview version does this in place with two pointers and O(1) extra memory.

Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @param {number} value
 * @return {number[]}
 */
var removeElement = function(nums, value) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Remove Element — Solution in Python, Java & C++

Remove Element solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on arrays and two pointers.

Remove Element is an easy coding problem on arrays and two pointers, asked in Amazon, Microsoft, TCS and Infosys interviews.

  • Difficulty: Easy
  • Topics: Arrays, Two Pointers
  • Asked at: Amazon, Microsoft, TCS, Infosys
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Given an integer array nums and an integer value, remove every occurrence of value from nums and return the remaining elements, keeping their original relative order.

Example 1

Input: nums = [3,2,2,3], value = 3
Output: [2,2]

Example 2

Input: nums = [0,1,2,2,3,0,4,2], value = 2
Output: [0,1,3,0,4]

Example 3

Input: nums = [1], value = 1
Output: []

Constraints

  • 0 <= nums.length <= 40
  • 0 <= nums[i] <= 50
  • 0 <= value <= 50

Follow-up: The interview version does this in place with two pointers and O(1) extra memory.

Hints for Remove Element

  1. Hint 1

    Use a read cursor and a write cursor over the same array.

  2. Hint 2

    Copy every element that is not value to the write position and advance it.

  3. Hint 3

    Everything past the write cursor at the end is leftover and does not matter.

How to solve Remove Element

Copy every element that is not value to a write cursor, preserving order.

Approach

  1. Walk the array with a read cursor.
  2. Whenever the element differs from value, write it at the write cursor and advance.

Why it works

The kept elements are written in the order they are read, so relative order survives, and everything past the write cursor is discarded.

Complexity

  • Time — O(n)
  • Space — O(1) extra in place

Pitfalls

  • The swap-with-last variant is fewer writes when value is rare, but does not preserve order.

Reference solution

Python

def removeElement(nums, value: int):
    out = []
    for x in nums:
        if x != value:
            out.append(x)
    return out

Java

public static int[] removeElement(int[] nums, int value) {
    int[] out = new int[nums.length];
    int len = 0;
    for (int v : nums) {
        if (v != value) out[len++] = v;
    }
    return Arrays.copyOf(out, len);
}

C++

vector<int> removeElement(vector<int>& nums, int value) {
    vector<int> out;
    for (int v : nums) {
        if (v != value) out.push_back(v);
    }
    return out;
}

JavaScript

var removeElement = function(nums, value) {
    const out = [];
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] !== value) out.push(nums[i]);
    }
    return out;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Two Pointers Technique