Logo

Programming-Idioms

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.
Implementation
Go

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Go implementation.

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.

Other implementations
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
	}
}
i = items.index(x)
items.delete_at(i) unless i.nil?
uses classes;
i := items.IndexOf(x);
if i <> -1 then
  items.delete(i);
import Data.List
delete x items
items.remove(x)
items.remove(x);
if let Some(i) = items.first(&x) {
    items.remove(i);
}
import std.algorithm;
items = items.remove(items.countUntil!(a => a == x));
use List::UtilsBy qw(extract_first_by);
extract_first_by { $x eq $_ } @items;
integer, dimension(:), allocatable :: items

i = findloc(items, x)
if (i /= 0) items = [items(1:i-1), items(i+1:)]
import java.util.List;
items.stream().findFirst().filter(item -> "x".equals(item)).ifPresent(removeIndex -> items.remove(removeIndex));
$list_position = array_search($x, $items);
$specific_item = $items[$position];
unset($specific_item);
using System.Collections.Generic;
items.Remove(x);
erase(items, x);
List.delete(items, x)
const idx = items.indexOf(x)
if (idx !== -1) items.splice(idx, 1)
(let [[n m]
      (split-with (partial not= x) items)]
  (concat n (rest m)))
import "slices"
func removeFirstByValue[S ~[]T, T comparable](items *S, x T) {
	for i, y := range *items {
		if y == x {
			*items = slices.Delete(*items, i, i+1)
			return
		}
	}
}
import java.util.ArrayList;
T value;
for(int index = 0; index < items.size(); index++) {
	value = items.get(index);
	if(value.equals(x)) {
		items.remove(index);
		break;
	}
}