# Program to check if two arrays are equal or not | faceprep

Program to check if two arrays are equal or not is discussed here. Given two arrays, display “Same” if both the arrays are equal. Else, display “Not Same”.

##### Input format:
• Input consists of 2 integers and 2 arrays.
• Integers correspond to the size of arrays.
• If two arrays are same, display “Same” else display “Not Same”

3
3
1
2
3
1
2
3

Same

## Algorithm to check if two arrays are equal or not

• Input the number of elements of arr1 and arr2.
• Input the elements of arr1 and arr2.
• If all the elements of arr1 and arr2 are equal, then print “Same”.
• Else, print “Not Same”.

Program to check if two arrays are equal or not is given below.

/* C Program to check if two arrays are equal or not */
#include<stdio.h>

int sort(int arr[], int n)
{
int i,j;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

int arrays_equal(int arr1[], int arr2[], int n, int m)
{
sort(arr1,n);
sort(arr2,m);
int i;
for(i = 0; i < n; i++)
{
if(arr1[i] != arr2[i])
{
return 0;
}
}
}

int main()
{
int n, m;
scanf(“%d”,&n);
scanf(“%d”,&m);
int arr1[n];
int arr2[m];
int i;
for(i = 0; i < n; i++)
{
scanf(“%d”,&arr1[i]);
}
for(i = 0; i < m; i++)
{
scanf(“%d”,&arr2[i]);
}
if(arrays_equal(arr1, arr2, n, m) == 0)
{
printf(“Not same”);
}
else
printf(“Same”);

return 0;
}

#include
#include
int same(int size1, int*array1, int size2, int*array2);
int main()
{

int *a,*b,n,p,i,c=0,d=0,e,f;
scanf(“%d%d”,&n,&p);
a=(int*)malloc(n*sizeof(int));
b=(int*)malloc(n*sizeof(int));
for(i=1;i<=n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=1;i<=p;i++)
{
scanf(“%d”,&b[i]);
}
for(i=1;i<=n;i++)
{
c=c+i;
c=e;
}
for(i=1;i<=p;i++)
{
d=d+i;
d=f;
}
if (e==f)
{
printf(“Same”);
}
else
{
printf(“Not Same”);
}
return 0;
}

/* C++ Program to check if two arrays are equal or not */
#include<bits/stdc++.h>
using namespace std;

int arrays_equal(int arr1[], int arr2[], int n, int m)
{
sort(arr1,arr1+n);
sort(arr2,arr2+m);
int i;
for(i = 0; i < n; i++)
{
if(arr1[i] != arr2[i])
{
return 0;
}
}
}

int main()
{
int n, m;
cin >> n >> m;
int arr1[n], arr2[m];
for(int i = 0; i < n; i++)
{
cin >> arr1[i];
}
for(int i = 0; i < m; i++)
{
cin >> arr2[i];
}
if(n != m)
cout << “Not Same”;
else
{
if(arrays_equal(arr1, arr2, n, m) == 0)
cout << “Not Same”;
else
cout << “Same”;
}

}

/* Java program to check if two arrays are equal or not */

import java.util.*;
public class Main
{
static void sort(int arr[], int n)
{
int i,j;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

static int arrays_equal(int arr1[], int arr2[], int n, int m)
{
sort(arr1,n);
sort(arr2,m);
int i, count = 0;
for(i = 0; i < n; i++)
{
if(arr1[i] == arr2[i])
{
count++;
}
}
return count;
}

public static void main(String[] args)
{
int n,m, count = 0;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
int[]arr1 = new int[n];
int[]arr2 = new int[n];
for(int i = 0; i < n; i++)
{
arr1[i] = sc.nextInt();
}
for(int i = 0; i < m; i++)
{
arr2[i] = sc.nextInt();
}

if(arrays_equal(arr1, arr2, n, m) != n)
{
System.out.print(“Not same”);
}
else
System.out.print(“Same”);

}
}

# Python program to check if two arrays are equal or not

def arrays_equal(arr1, arr2, n, m):
count = 0
arr1.sort()
arr2.sort()
for i in range(0,n):
if(arr1[i] == arr2[i]):
count = count + 1
print(count)
return count

n = int(input())
m = int(input())
arr1 = []
arr2 = []
for i in range(0,n):
temp = int(input())
arr1.append(temp)

for i in range(0,m):
temp = int(input())
arr2.append(temp)

if(arrays_equal(arr1, arr2, n, m) != n):
print(“Not Same”)
else:
print(“Same”)

Output: