Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an element is in an array

Tags:

arrays

swift

In Swift, how can I check if an element exists in an array? Xcode does not have any suggestions for contain, include, or has, and a quick search through the book turned up nothing. Any idea how to check for this? I know that there is a method find that returns the index number, but is there a method that returns a boolean like ruby's #include??

Example of what I need:

var elements = [1,2,3,4,5] if elements.contains(5) {   //do something } 
like image 893
jaredsmith Avatar asked Jun 08 '14 00:06

jaredsmith


People also ask

How do you check if an item is in an array in JavaScript?

Answer: Use the Array. isArray() Method You can use the JavaScript Array. isArray() method to check whether an object (or a variable) is an array or not. This method returns true if the value is an array; otherwise returns false .

How do you check if an element is in an array C?

To check if given Array contains a specified element in C programming, iterate over the elements of array, and during each iteration check if this element is equal to the element we are searching for.

How do you check if an element is in an array Python?

To check if an array contains an element or not in Python, use the in operator. The in operator checks whether a specified element is an integral element of a sequence like string, array, list, tuple, etc.

How do you check if an element is not in an array JavaScript?

To check if a value is not in array array, use the indexOf() method, e.g. arr. indexOf(myVar) === -1 . If the indexOf method returns -1 , then the value is not contained in the array.


2 Answers

Swift 2, 3, 4, 5:

let elements = [1, 2, 3, 4, 5] if elements.contains(5) {     print("yes") } 

contains() is a protocol extension method of SequenceType (for sequences of Equatable elements) and not a global method as in earlier releases.

Remarks:

  • This contains() method requires that the sequence elements adopt the Equatable protocol, compare e.g. Andrews's answer.
  • If the sequence elements are instances of a NSObject subclass then you have to override isEqual:, see NSObject subclass in Swift: hash vs hashValue, isEqual vs ==.
  • There is another – more general – contains() method which does not require the elements to be equatable and takes a predicate as an argument, see e.g. Shorthand to test if an object exists in an array for Swift?.

Swift older versions:

let elements = [1,2,3,4,5] if contains(elements, 5) {     println("yes") } 
like image 133
Martin R Avatar answered Oct 28 '22 17:10

Martin R


For those who came here looking for a find and remove an object from an array:

Swift 1

if let index = find(itemList, item) {     itemList.removeAtIndex(index) } 

Swift 2

if let index = itemList.indexOf(item) {     itemList.removeAtIndex(index) } 

Swift 3, 4

if let index = itemList.index(of: item) {     itemList.remove(at: index) } 

Swift 5.2

if let index = itemList.firstIndex(of: item) {     itemList.remove(at: index) } 
like image 45
DogCoffee Avatar answered Oct 28 '22 18:10

DogCoffee