This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
Idiom #135 Remove item from list, by its value
Remove at most 1 item from list items, having the value x.
This will alter the original list or return a new list, depending on which is more idiomatic.
If there are several occurrences of x in items, remove only one of them. If x is absent, keep items unchanged.
- Clojure
- C++
- C#
- D
- Dart
- Elixir
- Fortran
- Go
- Go
- Go
- Go
- Haskell
- JS
- Java
- Java
- Java
- PHP
- Pascal
- Perl
- Python
- Python
- Python
- Ruby
- Rust
for i, y := range items {
if y == x {
items = append(items[:i], items[i+1:]...)
break
}
}
First find a matching index i. Then remove at position i.
Warning: you may have a memory leak at the last element of the original list, if the items have a pointer type.
Warning: you may have a memory leak at the last element of the original list, if the items have a pointer type.
for i, y := range items {
if y == x {
copy(items[i:], items[i+1:])
items[len(items)-1] = nil
items = items[:len(items)-1]
break
}
}
First find a matching index i. Then remove at position i.
This code is for pointer value type, and has no memory leak.
This code is for pointer value type, and has no memory leak.
<T> void remove(List<T> items, T x) {
Iterator<T> i = items.listIterator();
while (i.hasNext())
if (i.next() == x) {
i.remove();
break;
}
}