This language bar is your friend. Select your favorite languages!

Idiom #136 Remove all occurrences of a value from a list

Remove all occurrences of value x from list items.
This will alter the original list or return a new list, depending on which is more idiomatic.

$newItems = array_diff($items, [$x]);
import std.algorithm.iteration;
import std.array;
items = items.filter!(a => a != x).array;
items2 := make([]T, 0, len(items))
for _, v := range items {
	if v != x {
		items2 = append(items2, v)
	}
}
j := 0
for i, v := range items {
	if v != x {
		items[j] = items[i]
		j++
	}
}
items = items[:j]
j := 0
for i, v := range items {
	if v != x {
		items[j] = items[i]
		j++
	}
}
for k := j; k < len(items); k++ {
	items[k] = nil
}
items = items[:j]
filter (/= x) items
var
  i: integer;

for i:= items.count-1 downto 0 do
  if items[i] = x then
    items.delete(i);
newlist = [item for item in items if item != x]
items.delete(x)
items = items.into_iter().filter(|&item| item != x).collect();

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms