Array Contains

From SwiftAPI

Jump to: navigation, search

Contents

C#

List<int> a = new List<int>() { 2, 1, 2, 5, 0 };
 
bool exists = a.Contains(5);
// exists == true
 
exists = a.Contains(3);
// exists == false

Java

for Lists:

import java.util.Arrays;
 
List<Integer> a = Arrays.asList( 2, 1, 2, 5, 0 );
 
boolean exists = a.contains(5);
// exists == true
 
exists = a.contains(3);
// exists == false

for arrays of objects (not primitive arrays):

import java.util.Arrays;
 
Integer[] b = { 2, 1, 2, 5, 0 };
List<Integer> a = Arrays.asList( b );
 
boolean exists = a.contains(5);
// exists == true
 
exists = a.contains(3);
// exists == false

JavaScript

JavaScript 1.6+

Firefox 1.5+

var a = [2, 1, 2, 5, 0];
var exists;
 
exists = a.indexOf(5) != -1;
// exists == true
 
exists = a.indexOf(3) != -1;
// exists == false

ES7

Mozilla

var a = [2, 1, 2, 5, 0];
var exists;
 
exists = a.includes(5);
// exists == true
 
exists = a.includes(3);
// exists == false


Prototype

prototype

var a = [2, 1, 2, 5, 0];
var exists;
 
exists = a.include(5);
// exists == true
 
exists = a.include(3);
// exists == false

MooTools

mootools

var a = [2, 1, 2, 5, 0];
var exists;
 
exists = a.contains(5);
// exists == true
 
exists = a.contains(3);
// exists == false

Objective-C

developer.apple.com

- (BOOL)containsObject:(id)anObject
NSArray *a = @[@2, @1, @2, @5, @0];
 
BOOL exists = [a containsObject:@5];
// exists == YES
 
exists = [a containsObject:@3];
// exists == NO

Perl

Perl 5.10+:

@a = (2, 1, 2, 5, 0);
 
$exists = 5 ~~ @a;
# $exists == 1
 
$exists = 3 ~~ @a;
# $exists == ''

PHP

php.net

$a = array(2, 1, 2, 5, 0);
 
$exists = in_array(5, $a);
// $exists == true
 
$exists = in_array(3, $a);
// $exists == false

Python

a = [2, 1, 2, 5, 0]
 
exists = 5 in a
# exists == True
 
exists = 3 in a
# exists == False

Ruby

Ruby-Doc.org

a = [2, 1, 2, 5, 0]
 
exists = a.include?(5)
# exists == true
 
exists = a.include?(3)
# exists == false

Swift

let a = [2, 1, 2, 5, 0]
 
var exists = contains(a, 5)
// exists == true
 
exists = contains(a, 3)
// exists == false
Personal tools