Swift Program to Check if Two Arrays Are Equal or Not


In this article, we will learn how to write a swift program to check whether two arrays are equal. To check if two arrays are equal to not either, we are going to use the following two methods 

  • using == operator

  • elementsEqual(_:) method.

Both method and operator return true if both the given array has the same elements. Otherwise, they will return false. For example, arr1 = [2, 3, 4, 5] and arr2 = [2, 3, 4, 5] both arrays are equal so both the method and operator will return true. arr1 = [2, 3, 4, 5] and arr2 = [1, 3, 3, 5] both arrays are not equal so both the method and operator will return false.

Method 1: Using == Operator

In the following example, we use the == operator to compare two arrays. If both arrays contain the same elements in the same order and of the same type, then this operator will return true. Otherwise, it will return false.

Syntax

Following is the syntax of == operator

Array1 == Array2

Algorithm

  • Step 1 − Create two arrays.

  • Step 2 − Check if both the array contain the same elements or not using either the == operator or elementsEqual(_:) method.

// Using == operator
var output1 = (Array1 == Array2)

// Using elementsEqual() method
var output = Array1.elementsEqual(Array2)
  • Step 3 − Print the output

Example

In the following example, we check if the given arrays are equal or not using the == operator.

import Foundation
import Glibc
 
// Creating arrays
var Array1 = [74, 99, 9, 38, 78, 132]
var Array2 = [74, 99, 9, 38, 78, 132]
var Array3 = [78, 99, 91, 38, 708, 32]

// Checking if the arrays are equal or not
// Using == operator
var output1 = (Array1 == Array2)
print("Is Array 1 is equal to Array 2?", output1)

var output2 = (Array1 == Array3)
print("Is Array 1 is equal to Array 3?", output2)

Output

Is Array 1 is equal to Array 2? true
Is Array 1 is equal to Array 3? false

Here in the above code, we create three arrays with different variations in their values. Now we compare arrays with each other using the == operator. It will return true if the array present on the left-hand side is equal to the array present on the right-hand side of this operator. Otherwise, return false.

Method 2: Using elementsEqual(_:) Method

The elementsEqual(_:) method will return true when both arrays contain the same elements in the same order. Otherwise, this method will return false. If the elements are the same but the order is different, then also this method will return false. Out of two given arrays at least one must be the finite array.

Syntax

Following is the syntax of elementsEqual(_:) method −

func elementsEqual<anotherSequence>(_other:Another Sequence)->Bool 

Here, other parameters represent the sequence to which we want to compare.

Algorithm

  • Step 1 − Create two arrays.

  • Step 2 − Check if both the array contain the same elements or not using the elementsEqual(_:) method.

var output = Array1.elementsEqual(Array2)
  • Step 3 − Print the output

Example

import Foundation
import Glibc
 
// Creating arrays
var Array1 = ["Bus", "Car", "bicycle", "bike"]
var Array2 = ["Bus", "Car", "bicycle", "bike"]
var Array3 = ["Mango", "Apple", "Banana", "Orange"]
var Array4 = ["Car", "Bus", "bike", "bicycle"]

// Checking if the arrays are equal or not
// Using elementsEqual() method
var output1 = Array1.elementsEqual(Array2)
print("Is Array 1 is equal to Array 2?", output1)

var output2 = Array1.elementsEqual(Array3)
print("Is Array 1 is equal to Array 3?", output2)

var output3 = Array1.elementsEqual(Array4)
print("Is Array 1 is equal to Array 4?", output3)

In the following example, we use the elementsEqual(_:) method to check if two arrays are equal or not.

Output

Is Array 1 is equal to Array 2? true
Is Array 1 is equal to Array 3? false
Is Array 1 is equal to Array 4? false

Here in the above code, we create four arrays with different variations in their values. Now we compare arrays with each other using elementsEqual(_:) method. It will return true if the Array on which this method is applied is equal to the array present inside the method(as a parameter) are equal. Otherwise, return false.

Conclusion

So, this is how we can check if two arrays are equal or not either using == operator or using elementsEqual() method. They both return the same result so you can use them according to your choice. Also, they compare two arrays at a time.

Updated on: 20-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements