# Write Program to check if two arrays are the same or not

09 June 2022

Write Program to check if two arrays are the same or not

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

# Description

Get two arrays as the input from the user and check whether it is the same or not.

Input

Enter the size of first array: 3

Enter the size of second array: 3

Enter elements of first array: 1 2 3

Enter elements of second array: 1 2 3

Output

Same

C Program

#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(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 n1, n2;

printf("Enter the size of first array: ");

scanf("%d",&n1);

printf("Enter the size of second array: ");

scanf("%d",&n2);

int arr1[n1];

int arr2[n2];

int i;

printf("Enter the first array elements: ");

for(i = 0; i < n1; i++)

{

scanf("%d",&arr1[i]);

}

printf("Enter the second array elements: ");

for(i = 0; i < n2; i++)

{

scanf("%d",&arr2[i]);

}

if(arrays(arr1, arr2, n1, n2) == 0)

{

printf("Not same");

}

else

printf("Same");

return 0;

}

C++ Program

#include<iostream>

using namespace std;

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(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 n1, n2;

cout<<"Enter the size of first array: ";

cin>>n1;

cout<<"Enter the size of second array: ";

cin>>n2;

int arr1[n1];

int arr2[n2];

int i;

cout<<"Enter the first array elements: ";

for(i = 0; i < n1; i++)

{

cin>>arr1[i];

}

cout<<"Enter the second array elements: ";

for(i = 0; i < n2; i++)

{

cin>>arr2[i];

}

if(arrays(arr1, arr2, n1, n2) == 0)

{

cout<<"Not same";

}

else

cout<<"Same";

return 0;

}

Java Program

import java.util.*;

public class Main

{

static void sort(int arr[], int n1)

{

int i,j;

for (i = 0; i < n1-1; i++)

{

for (j = 0; j < n1-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(int arr1[], int arr2[], int n1, int n2)

{

sort(arr1,n1);

sort(arr2,n2);

int i, count = 0;

for(i = 0; i < n1; i++)

{

if(arr1[i] == arr2[i])

{

count++;

}

}

return count;

}

public static void main(String[] args)

{

int n1,n2, count = 0;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of first array: ");

n1 = sc.nextInt();

System.out.println("Enter the size of second array: ");

n2 = sc.nextInt();

int[]arr1 = new int[n1];

int[]arr2 = new int[n1];

System.out.println("Enter the elements of first array: ");

for(int i = 0; i < n1; i++)

{

arr1[i] = sc.nextInt();

}

System.out.println("Enter the elements of second array: ");

for(int i = 0; i < n2; i++)

{

arr2[i] = sc.nextInt();

}

if(arrays(arr1, arr2, n1, n2) != n1)

{

System.out.print("Not same");

}

else

System.out.print("Same");

}

}

Python Program

def arrays(arr1, arr2, n1, n2):

count = 0

arr1.sort()

arr2.sort()

for i in range(0,n1):

if(arr1[i] == arr2[i]):

count = count + 1

print(count)

return count

n1 = int(input("Enter the size of first array: "))

n2 = int(input("Enter the size of second array: "))

arr1 = []

arr2 = []

print("Enter the elements of first array: ")

for i in range(0,n1):

temp = int(input())

arr1.append(temp)

print("Enter the elements of second array: ")

for i in range(0,n2):

temp = int(input())

arr2.append(temp)

if(arrays(arr1, arr2, n1, n2) != n1):

print("Not Same")

else:

print("Same")

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023