# How to Compare Arrays for equality in C++?

In this article, we will discuss how to compare two arrays in C++.

## Problem Description

Here, we are given two arrays, and we have to find if they are equal or not.

Input

```int arr1[] = {12, 56, 823, 7, 1023};
int arr2[] = {12, 56, 823, 7, 1023};```

Output

`Both are equal`

There are two method to compare array for equality in C++. Let’s see how we can do it,

## Compare Arrays using Linear traversal (Iterative Method)

In this method, we will iterate the whole array

These are following steps :

1. Firstly we check the size of both array.
2. If size are not equal then array are not equal.
3. Initialize a variable isequal with true
4. If size are equal then iterate a for loop and compare all values of an array with another array
5. If we find any index where values are not equal break the loop and set the value of isequal to false.

`Time Complexity: O(n)`
`Space Complexity: O(1)`

### Best Resources to Learn C++:

Example

```// C++ program to Compare array for equality

#include <iostream>

using namespace std;

// Driver Code
int main()
{
int arr1[] = {12, 56, 823, 7, 1023};
int arr2[] = {12, 56, 823, 7, 1023};

// n1 is the size of array 1
int n1 = sizeof(arr1) / sizeof(arr1);

// n2 is the size of array 2
int n2 = sizeof(arr2) / sizeof(arr2);

//Intialize a variable
bool isequal = true;

if(n1 != n2)
{
isequal = false;
}
else
{
// Iterate over the array
for(int i=0; i < n1; i++)
{
if(arr1[i] != arr2[i])
{
isequal = false;
break;
}
}
}

if(isequal)
{
cout<<"Both Arrays are equal" << endl;
}
else
{
cout<<"Both Arrays are not equal" << endl;
}
return 0;
}```

Output

```Both Arrays are equal
```

Let’s take another example where array are not equal

```// C++ program to Compare array for equality

#include <iostream>

using namespace std;

// Driver Code
int main()
{
int arr1[] = {12, 56, 823, 17, 1023};
int arr2[] = {12, 56, 823, 7, 1023};

// n1 is the size of array 1
int n1 = sizeof(arr1) / sizeof(arr1);

// n2 is the size of array 2
int n2 = sizeof(arr2) / sizeof(arr2);

//Intialize a variable
bool isequal = true;

if(n1 != n2)
{
isequal = false;
}
else
{
// Iterate over the array
for(int i=0; i < n1; i++)
{
if(arr1[i] != arr2[i])
{
isequal = false;
break;
}
}
}

if(isequal)
{
cout<<"Both Arrays are equal" << endl;
}
else
{
cout<<"Both Arrays are not equal" << endl;
}
return 0;
}```

Output

```Both Arrays are not equal
```

## Compare Arrays using STL function equal()

In this method we use a STL function to compare array for equality.

STL function is : equal()

`bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);`

It compares the element in range [first1,last1) with the elements in range starting from first2.

`Time Complexity: O(n)`
`Space Complexity: O(1)`

Example

```// STL Function to compare array for equality
#include <iostream>
using namespace std;

// Driver Code
int main()
{
int arr1[] = {12, 56, 823, 7, 1023};
int arr2[] = {12, 56, 823, 7, 1023};

// n1 is the size of array 1
int n1 = sizeof(arr1) / sizeof(arr1);

// n2 is the size of array 2
int n2 = sizeof(arr2) / sizeof(arr2);

// equal() function return bool
// 1 if both are equal
// 0 of both are not equal
if(equal(arr1, arr1 + n1, arr2))
{
cout<<"Both Arrays are equal" << endl;
}
else
{
cout<<"Both Arrays are not equal" << endl;
}
return 0;
}```

Output

`Both Arrays are equal`

Let’s take another example where element doestn’t exist

```// STL Function to compare array for equality
#include <iostream>
using namespace std;

// Driver Code
int main()
{
int arr1[] = {12, 56, 823, 27, 1023};
int arr2[] = {12, 56, 823, 7, 1023};

// n1 is the size of array 1
int n1 = sizeof(arr1) / sizeof(arr1);

// n2 is the size of array 2
int n2 = sizeof(arr2) / sizeof(arr2);

// equal() function return bool
// 1 if both are equal
// 0 of both are not equal
if(equal(arr1, arr1 + n1, arr2))
{
cout<<"Both Arrays are equal" << endl;
}
else
{
cout<<"Both Arrays are not equal" << endl;
}

return 0;
}```

Output

```Both Arrays are not equal
```

## Summary

We have seen two different method to compare arrays for equality in C++. One is the naive solution. Another one is using the STL function equal(). Every Method has it’s own time complexity and space complexity.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top