
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
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.
- Related Articles
- Golang Program To Find Common Array Elements
- Swift Program to get array elements after N elements
- Swift Program to Rotate Elements of an Array
- Swift Program to Remove Duplicate Elements From an Array
- Swift Program to Count the elements of an Array
- Swift Program to Shuffle the Elements of an Array
- Swift Program to Remove All the Elements from the Array
- Swift Program to Copy All the Elements of One Array to Another Array
- Java Program to Find Common Elements in Two ArrayList
- Java Program to Find Common Elements Between Two Arrays
- Swift Program to Find Mean of an Unsorted Array
- Swift Program to Find Median of an Unsorted Array
- Swift Program to fetch elements from an array based on an index
- Swift Program to remove all 'nil' elements from the array
- Swift Program to Sort the Elements of an Array in Ascending Order
