# Swift Program to Find Common Array Elements

This article will teach us how to write a swift program to find common array elements. So here we use the following methods

• Using the filter method

• Using sets

But before moving to the methods, we first understand common elements with the help of an example. Suppose we have two arrays −

Arr1 = [2, 4, 6, 89, 78]
Arr2 = [56, 88, 32, 4, 99, 89]


So the common elements between two arrays are [4, 89]. Because they are available in Arr1 and Arr2.

## Method 1: Using filter(_:) Method

To find common elements between two arrays we use a pre-defined function named filter(_:). In this function, we return a new array that contains all the elements of the given sequence that satisfy the given condition.

### Syntax

Following is the syntax −

func filter(_isInput:(Self.Element) throws->Bool)rethrows->[Self.Element]


Here isInput is a closure that takes an item of the given sequence as its argument and returns a boolean value representing whether the given item is removed from the given sequence.

### Algorithm

• Step 1 − Create two arrays of the same type.

• Step 2 − Find the common elements between these two arrays using filter() and contains() methods and assign the result to a new variable.

var commonArray = sequence1.filter{sequence2.contains($0)} Or var commonArray = sequence1.filter{sequence2.contains}  • Step 3 − Print the output ### Example In the following example, we find common elements between two arrays using the filter() method. import Foundation import Glibc // Creating two arrays of integer type var sequence1 : [Int] = [2, 4, 6, 7, 45, 67, 9, 90] var sequence2 : [Int] = [21, 44, 346, 7, 45, 89, 9, 90, 23] // Finding the common elements using filter() method var commonArray = sequence1.filter{sequence2.contains($0)}

print("Array 1:", sequence1)
print("Array 2:", sequence2)
print("Common Elements between both arrays:", commonArray)


### Output

Array 1: [2, 4, 6, 7, 45, 67, 9, 90]
Array 2: [21, 44, 346, 7, 45, 89, 9, 90, 23]
Common Elements between both arrays: [7, 45, 9, 90]


Here in the above code, we create two arrays of integer type. Now we find the common elements in between them using filter() and contains() methods and store the result into a new array. So we apply the filter method to the first array and inside the filter() method, we apply contains() function on the second array to check whether the element of the first array is available in the second array starting from index 0(\$0) to the end.

## Method 2: Using Set

We can also find common elements between two arrays using the Set intersection(_:) method. This function is used to find common elements between two sequences. This method is generally used on Sets so to use this method on an array first we convert the given arrays into sets and then convert the result back into the array.

### Syntax

Following is the syntax −

func intersection(_other:Set)->Set


Here other is another set.

### Algorithm

• Step 1 − Create two arrays of the same type.

• Step 2 − Find the common elements between these two arrays using the intersection() method. So we convert both the arrays into sets and then convert the result back into the array.

var commonArray = Array(Set(sequence1).intersection(Set(sequence2)))

• Step 3 − If the condition is true print the array is empty.

### Example

In the following example, we find common elements between two arrays using the Set intersection() method.

import Foundation
import Glibc

// Creating two arrays of integer type
var sequence1 : [Int] = [4, 32, 6, 32, 2, 45]
var sequence2 : [Int] = [21, 44, 6, 2, 45, 89, 4]

// Finding the common elements using intersection() method
var commonArray = Array(Set(sequence1).intersection(Set(sequence2)))

print("Array 1:", sequence1)
print("Array 2:", sequence2)

print("Common Elements between both arrays:", commonArray)


### Output

Array 1: [4, 32, 6, 32, 2, 45]
Array 2: [21, 44, 6, 2, 45, 89, 4]
Common Elements between both arrays: [6, 45, 2, 4]


Here in the above code, we create two arrays of integer type. Now we find the common elements between them using the intersection() method. So we convert both arrays into a set using the Set() method and then apply the intersection() method on both sets and convert the result back into an array using Array().

## Conclusion

Hence to find common elements between two arrays we can use either filter() or intersection() methods according to your requirement. Both methods complete the given task very well.