Skip to content
DeveloperMemos

Checking if a Swift Array Contains Any of The Elements of Another Array

Swift, Arrays, Development1 min read

When working with arrays in Swift, it's common to encounter situations where you need to check if an array contains any of the elements from another array. This can be particularly useful when developing applications that involve data filtering, validation, or comparison. In this article, we'll explore different methods to achieve this in Swift, providing practical examples along the way.

Using Set Intersection

One efficient way to determine if a Swift array contains any elements of another array is by utilizing the set intersection approach. This method leverages the Set type's built-in capability to perform intersection operations.

Let's consider two arrays, firstArray and secondArray, and we want to check if any elements in secondArray are present in firstArray. We can accomplish this as follows:

1let firstArray = [1, 2, 3, 4, 5]
2let secondArray = [3, 6, 7]
3
4let intersection = Set(firstArray).intersection(Set(secondArray))
5if intersection.isEmpty {
6 print("No common elements found")
7} else {
8 print("Common elements found: \(intersection)")
9}

In this example, the Set constructor is used to convert both arrays into sets, which automatically removes duplicate elements. The intersection function then retrieves the common elements between the two sets. If the resulting intersection set is empty, it means no common elements were found. Otherwise, the common elements are printed.

Utilizing the contains(where:) Method

Another approach involves using the contains(where:) method to iterate through one array and check for the presence of any of its elements in another array.

Consider the following example:

1let languages = ["Swift", "Kotlin", "Java", "JavaScript"]
2let userInterests = ["Python", "JavaScript", "Ruby"]
3
4let containsCommonElement = userInterests.contains { languages.contains($0) }
5if containsCommonElement {
6 print("Common elements found")
7} else {
8 print("No common elements found")
9}

In this snippet, the contains method is employed to iterate through userInterests, checking if any of its elements are present in the languages array. If a match is found, the message "Common elements found" is printed; otherwise, "No common elements found" is displayed.

Using firstIndex(where:) to Find the First Match

If you're interested in finding the index of the first matching element rather than simply determining whether common elements exist, the firstIndex(where:) method can be utilized. This method returns the index of the first element that satisfies the given predicate.

Here's an example illustrating its use:

1let numbers = [10, 20, 30, 40, 50]
2let searchValues = [35, 40, 55]
3
4if let firstMatchingIndex = numbers.firstIndex(where: { searchValues.contains($0) }) {
5 print("First common element found at index: \(firstMatchingIndex)")
6} else {
7 print("No common elements found")
8}

In this case, the firstIndex method is employed to find the index of the first common element between the numbers array and the searchValues array. If a match is found, the index is printed; otherwise, the message "No common elements found" is displayed.

By employing these methods, you can efficiently check if a Swift array contains any elements of another array, catering to a wide range of programming scenarios.