3

I am trying to do the simple task of finding if a (string) element exists in an array. The "contains" function works with a one-dimensional array, but not in a 2-dimensional array. Any suggestions? (Documentation on this function appears to be sparse, or I don't know where to look.)

5 Answers 5

3

The Swift standard library does not have "multi-dimensional arrays", but if you refer to "nested arrays" (i.e. an array of arrays) then a nested contains() would work, for example:

let array = [["a", "b"], ["c", "d"], ["e", "f"]]
let c = array.contains { $0.contains("d") }
print(c) // true

Here the inner contains() method is

public func contains(element: Self.Generator.Element) -> Bool

and the outer contains() method is the predicate-based

public func contains(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Bool

which returns true as soon as the given element is found in one of the inner arrays.

This approach can be generalized to deeper nesting levels.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! The Swift documentation does refer to multidimensional arrays but doesn't give much information on using them.
3

Updated for Swift 3

The flatten method has been renamed to joined now. So the usage would be

[[1, 2], [3, 4], [5, 6]].joined().contains(3) // true

For multi-dimensional array, you can use flatten to decrease one dimension. So for two-dimensional array:

[[1, 2], [3, 4], [5, 6]].flatten().contains(7) // false

[[1, 2], [3, 4], [5, 6]].flatten().contains(3) // true

Comments

1

Not as good as J.Wangs answer but another alternative - You can reduce the list to a single boolean value using the reduce(,combine:) function.

[[1,2], [3,4], [5,6]].reduce(false, combine: {$0 || $1.contains(4)})

Comments

0

You can also write an extension (Swift 3):

extension Sequence where Iterator.Element: Sequence {
    func contains2D(where predicate: (Self.Iterator.Element.Iterator.Element) throws -> Bool) rethrows -> Bool {
        return try contains(where: {
            try $0.contains(where: predicate)
        })
    }
}

Comments

-1
let array = [["a", "b"], ["c", "d"], ["e", "f"]]
var c = array.contains { $0.contains("d") }
print(c) // true

c = array.contains{$0[1] == "d"}
print(c)  //  true

c = array.contains{$0[0] == "c"}
print (c) //  true


if let indexOfC = array.firstIndex(where: {$0[1] == "d"}) {
    print(array[indexOfC][0]) // c
    print(array[indexOfC][1]) // d
} else  {
    print ("Sorry, letter is not in position [][letter]")
}

if let indexOfC = array.firstIndex(where: {$0[0] == "c"}) {
    print(array[indexOfC][1]) //  d
    print(array[indexOfC][0]) //  c
} else  {
    print ("Sorry, letter is not in position [letter][]")
}

1 Comment

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.