Idiom #136 Remove all occurrences of a value from a list
Remove all occurrences of the value x from list items.
This will alter the original list or return a new list, depending on which is more idiomatic.
Remove all occurrences of the value x from list items.
This will alter the original list or return a new list, depending on which is more idiomatic.
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]
items2 := make([]T, 0, len(items))
for _, v := range items {
if v != x {
items2 = append(items2, v)
}
}
func removeAll[S ~[]T, T comparable](items *S, x T) {
j := 0
for i, v := range *items {
if v != x {
(*items)[j] = (*items)[i]
j++
}
}
var zero T
for k := j; k < len(*items); k++ {
(*items)[k] = zero
}
*items = (*items)[:j]
}
filter (/= x) items
items.removeAll(Collections.singleton(x));
Iterator<T> i = items.listIterator();
while (i.hasNext())
if (i.next().equals(x)) i.remove();
(remove-if (lambda (val) (= val x)) items)
(remove #{x} items)
items.remove(x);
items.RemoveAll(r => r == x);
items = items.filter!(a => a != x).array;
items.removeWhere((y)=>y==x);
Enum.filter(items, fn v -> v != x end)
items = pack (items,items != x)
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]
items2 := make([]T, 0, len(items)) for _, v := range items { if v != x { items2 = append(items2, v) } }
func removeAll[S ~[]T, T comparable](items *S, x T) { j := 0 for i, v := range *items { if v != x { (*items)[j] = (*items)[i] j++ } } var zero T for k := j; k < len(*items); k++ { (*items)[k] = zero } *items = (*items)[:j] }
items = slices.DeleteFunc(items, func(e T) bool { return e == x })
filter (/= x) items
const newlist = items.filter(y => x !== y)
items.removeAll(Collections.singleton(x));
Iterator<T> i = items.listIterator(); while (i.hasNext()) if (i.next().equals(x)) i.remove();
items.removeIf(t -> t.equals(x));
$newItems = array_diff($items, [$x]);
var i: integer; for i:= items.count-1 downto 0 do if items[i] = x then items.delete(i);
my @filtered = grep { $x ne $_ } @items;
items = [a for a in items if a != x]
i, n = 0, len(items) while i != n: if items[i] == x: del items[i] n = n - 1 else: i = i + 1
newlist = [item for item in items if item != x]
f = lambda a: a != x items = list(filter(f, items))
while items.count(x): items.remove(x)
items = list(a for a in items if a != x)
items.delete(x)
items = items.into_iter().filter(|&item| item != x).collect();
items.retain(|&item| item != x);
items.filter(_ != x)
items reject: [: y | y = x ]