Logo

Programming-Idioms

Check if the list contains the value x.
list is an iterable finite container.
Implementation
Perl

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 Perl 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
func Contains(list []T, x T) bool {
	for _, item := range list {
		if item == x {
			return true
		}
	}
	return false
}
#include <vector>
#include <algorithm>
bool Contains(const std::vector<int> &list, int x)
{
	return std::find(list.begin(), list.end(), x) != list.end();
}
(define (contains list x)
	(cond [(null? list) #f]
		[(equal? (car list) x) #t]
		[else (contains (cdr list) x)]))
x in list
boolean contains(int[] list, int x){
  for(int y:list)
    if( y==x )
      return true;
  return false;
}
boolean <T> contains(T[] list, T x){
  if( x==null){
    for(T y:list)
      if( y==null )
        return true;
  }else{
    for(T y:list)
      if( x.equals(y) )
        return true;
  }
  return false;
}
import java.util.List;
list.contains(x)
return list.indexOf(x) !== -1;
list.contains(&x);
in_array($x, $list, true);
import std.algorithm.searching;
bool here = canFind(items, x);
list.contains(x);
result := false;
for e in list do 
	if e=x then
		begin
			result := true;
			break;
		end
uses classes;
result := list.IndexOf(x) <> -1;
x `elem` list
(some (partial = x) list)
Enum.member?(list, x)
lists:member(X, List).
#include <stdbool.h>
bool contains(int x, int* list, size_t list_len) {
    for (int i=0 ; i<list_len ; i++)
        if (list[i] == x)
            return true;
    return false;
}
function contains(list, x)
	for _, v in ipairs(list) do
		if v == x then return true end
	end
	return false
end
with Ada.Containers.Vectors;
Result := List.Contains (X);
System.Collections.Generic
list.Contains(item);
list.contains(x)
return list.includes(x);
list.iter().any(|v| v == &x)
(&list).into_iter().any(|v| v == &x)
member(_, []) -> false;
member(Value, [H|T]) -> 
  case H of
    Value -> true;
    _ -> member(T)
  end.
member(_, []) -> false;
member(Value, [H|_]) where Value =:= H -> true;
member(Value, [_|T]) -> member(Value, T).
[list containsObject:x];
list.include? x
use List::Util 'first';
print "ok\n" if first {$_ eq $x} @list;
(member x list)
(some #{x} list)
List.mem x list
x in list
if (findloc (list, x, 1) != 0) then
member(X, [One]).
List.Contains(x)
if (any(x == list)) ...
find _ [] = False
find n (x:xs)
  | x == n = True
  | otherwise = find n xs
x in list
list.contains(x)
list.contains(x)
(member x list)
p := list;
while (p <> nil) and (p^.key = x) do p := p^.next;
found := p.key = x
list includes: x.
(find x list :test #'equal)
import "slices"
slices.Contains(list, x)
#include <ranges>
auto contains(auto list, auto x) -> bool {
  return std::ranges::find(list, x) != std::ranges::end(list);
}
list.binary_search(&x).is_ok()