# Programming-Idioms

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

# Idiom #12 Check if list contains a value

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

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

programming-idioms.org