Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Clojure
- C++
- C#
- D
- Dart
- Elixir
- Fortran
- Go
- Go
- Go
- Go
- Go
- Haskell
- JS
- Java
- Java
- Java
- Lisp
- PHP
- Pascal
- Perl
- Python
- Python
- Python
- Python
- Python
- Python
- Ruby
- Rust
- Rust
- Scala
- Smalltalk
(remove #{x} items)
Puts the x value in a set that serves as simple predicate
items.remove(x);
C++20.
items.removeWhere((y)=>y==x);
Enum.filter(items, fn v -> v != x end)
Negating the search x removes it
items = pack (items,items != x)
items2 := make([]T, 0, len(items))
for _, v := range items {
if v != x {
items2 = append(items2, v)
}
}
This is simple and runs in linear time.
However, it allocates memory for the new slice items2.
T is the type of the elements.
However, it allocates memory for the new slice items2.
T is the type of the elements.
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]
}
The type parameter T has a constraint: it must be comparable with ==
In case T contains pointers, zeroing discarded elements helps garbage collection.
In case T contains pointers, zeroing discarded elements helps garbage collection.
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]
This filters items in-place in linear time.
The "tail" elements are set to nil to leverage garbage collection, avoiding a memory leak.
The "tail" elements are set to nil to leverage garbage collection, avoiding a memory leak.
j := 0
for i, v := range items {
if v != x {
items[j] = items[i]
j++
}
}
items = items[:j]
This filters items in-place in linear time.
But don't use it with pointer elements, because you would have a memory leak at the end of the underlying array.
But don't use it with pointer elements, because you would have a memory leak at the end of the underlying array.
filter (/= x) items
This returns a new list.
/= is the inequality operator of Haskell. Note that its usage requires x's type to instantiate the Eq type-class.
(/= x) is a shorthand for the lambda (\e -> e /= x).
/= is the inequality operator of Haskell. Note that its usage requires x's type to instantiate the Eq type-class.
(/= x) is a shorthand for the lambda (\e -> e /= x).
const newlist = items.filter(y => x !== y)
items.removeAll(Collections.singleton(x));
items has type java.util.List.
items is altered by removeAll().
items is altered by removeAll().
items.removeIf(t -> t.equals(x));
Iterator<T> i = items.listIterator();
while (i.hasNext())
if (i.next().equals(x)) i.remove();
(remove-if (lambda (val) (= val x)) items)
Functional implementation that returns a new list. = function assumes that items is a list of numbers.
var
i: integer;
for i:= items.count-1 downto 0 do
if items[i] = x then
items.delete(i);
Works with TList, TObjectList, TStringList, etc.
We start from the end and delete each occurrence.
This is O(n²): very slow.
We start from the end and delete each occurrence.
This is O(n²): very slow.
my @filtered = grep { $x ne $_ } @items;
items = [a for a in items if a != x]
newlist = [item for item in items if item != x]
items = list(a for a in items if a != x)
while items.count(x):
items.remove(x)
f = lambda a: a != x
items = list(filter(f, items))
i, n = 0, len(items)
while i != n:
if items[i] == x:
del items[i]
n = n - 1
else:
i = i + 1
items.filter(_ != x)
This will return a new list with all the occurrences of x removed.
The order of the elements is preserved.
The order of the elements is preserved.
items reject: [: y | y = x ]
returns a new list