Groovy list contains

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • C
  • C++
  • C#
  • Go
  • Java
  • JS
  • Obj-C
  • PHP
  • Python
  • Ruby
  • Rust
  • Or search :

Idiom #12 Check if list contains a value

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

Groovy list contains
  • Groovy
  • Ada
  • C
  • Caml
  • Clojure
  • Clojure
  • C++
  • C#
  • D
  • Dart
  • Elixir
  • Elixir
  • Erlang
  • Erlang
  • Erlang
  • Fortran
  • Fortran
  • Go
  • Haskell
  • Haskell
  • JS
  • JS
  • Java
  • Java
  • Java
  • Kotlin
  • Kotlin
  • Lisp
  • Lua
  • Lua
  • Obj-C
  • PHP
  • Pascal
  • Pascal
  • Pascal
  • Perl
  • Perl
  • Prolog
  • Python
  • Ruby
  • Rust
  • Rust
  • Rust
  • Scala
  • Scheme
  • Scheme
  • Smalltalk
  • VB
  • Groovy
list.contains(x)
  • Demo
  • Ada
  • C
  • Caml
  • Clojure
  • Clojure
  • C++
  • C#
  • D
  • Dart
  • Elixir
  • Elixir
  • Erlang
  • Erlang
  • Erlang
  • Fortran
  • Fortran
  • Go
  • Haskell
  • Haskell
  • JS
  • JS
  • Java
  • Java
  • Java
  • Kotlin
  • Kotlin
  • Lisp
  • Lua
  • Lua
  • Obj-C
  • PHP
  • Pascal
  • Pascal
  • Pascal
  • Perl
  • Perl
  • Prolog
  • Python
  • Ruby
  • Rust
  • Rust
  • Rust
  • Scala
  • Scheme
  • Scheme
  • Smalltalk
  • VB
with Ada.Containers.Vectors;
Result := List.Contains (X);
#include
bool contains(int x, int* list, size_t list_len) { for (int i=0 ; i
List.mem x list
(some (partial = x) list)
  • Doc
(some #{x} list)
#include #include
bool Contains(const std::vector &list, int x) { return std::find(list.begin(), list.end(), x) != list.end(); }
  • Doc
System.Collections.Generic
list.Contains(item);
import std.algorithm.searching;
bool here = canFind(items, x);
list.contains(x);
  • Demo
  • Doc
x in list
Enum.member?(list, x)
  • Doc
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.
lists:member(X, List).
  • Doc
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 }
  • Demo
  • Doc
x `elem` list
  • Demo
  • Doc
find _ [] = False find n (x:xs) | x == n = True | otherwise = find n xs
  • Demo
return list.indexOf(x) !== -1;
  • Doc
return list.includes(x);
  • Doc
import java.util.List;
list.contains(x)
boolean contains(int[] list, int x){ for(int y:list) if( y==x ) return true; return false; }
boolean 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; }
list.contains(x)
  • Demo
x in list
  • Demo
(member x list)
function contains(list, x) for _, v in pairs(list) do if v == x then return true end end return false end
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);
uses classes;
result := list.IndexOf(x) <> -1;
result := false; for e in list do if e=x then begin result := true; break; end
p := list; while (p <> nil) cand (p^.key = x) do p := p^.next; found := p.key = x
use List::Util 'first';
print "ok\n" if first {$_ eq $x} @list;
  • Demo
  • Doc
print "Found 'foo'\n" if grep {$_ eq $x} @list;
  • Demo
member(X, [One]).
  • Doc
  • Origin
x in list
list.include? x
  • Doc
list.contains(&x);
  • Demo
  • Doc
list.iter().any(|v| v == &x)
  • Demo
  • Doc
(&list).into_iter().any(|v| v == &x)
  • Demo
  • Doc
list.contains(x)
(define (contains list x) (cond [(null? list) #f] [(equal? (car list) x) #t] [else (contains (cdr list) x)]))
  • Demo
(member x list)
  • Doc
list includes: x.
  • Demo
List.Contains(x)

Do you know the best way to do this in your language ? New implementation...
Idiom created by programming-idioms.org
History
  • View revisions
Related idioms
  • Check if map contains value
  • Find first index of an element in list
  • Replace value in list
  • Check if set contains a value
Cheatsheets
Issues
  • Report a bug