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

Idiom #135 Remove item from list, by its value

Remove at most 1 item from list items, having 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 let Some(i) = items.first(&x) {
    items.remove(i);
}
items.remove(x);
for i, y := range items {
	if y == x {
		items = append(items[:i], items[i+1:]...)
		break
	}
}
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
	}
}
removeOne :: Eq a => a -> [a] -> [a]
removeOne _ [] = []
removeOne x (i:items)
   | x == i = items
   | otherwise = i : (removeOne x items)
uses classes;
i := items.IndexOf(c);
if i <> -1 then
  items.delete(i);
items.remove(x)
i = items.index(x)
items.delete_at(i) unless i.nil?

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

Idiom created by

programming-idioms.org

Related idioms