Check if two arrays are same or not
Check if two arrays are the same or not
Here we want to check whether two arrays are the same or not. The two arrays are the same when their length is equal.
so for checking that both arrays are same or not, we need to calculate the length of both array and then check the length is same or not.
Two arrays are said to be equal if both of them contain the same set of elements, arguments of elements though.
here we sort the array because it makes a comparison essay.
now we will discuss the working of the program and also discuss the program of this.
Working
Step 1. Initialize two arrays.
Step 2. Declare the scanner class for taking input.
Step 3. take array size for both arrays from the user.
Step 4. Take an element of both array from the user.
Step 5. Check that equal function return true or false
Step 6. If true then both arrays are same otherwise arrays ara not same
equal(int a1[], int a2[])
Step 1. Find the length of both array
Step 2. If the length of both arrays is not equal then return false.
Step 3. Sort both arrays.
Step 4. Initialise the for loop from 0 to length for check the element is same or not.
Step 5. If any element of the array is not equal to another array so return false.
Step 6. Return true.
#include <stdio.h>
int main()
{
int n1, n2, i , j, count = 0;
printf("enter size of array 1 : ");
scanf("%d",&n1);
int arr1[n1];
printf("enter elements of array 1 : ");
for(i=0; i<n1; i++)
{
scanf("%d",&arr1[i]);
}
printf("enter size of array 2 : ");
scanf("%d",&n2);
int arr2[n2];
printf("enter elements of array 2 : ");
for(i=0; i<n2; i++)
{
scanf("%d",&arr2[i]);
}
for(i=0; i<n1; i++)
{
for(j=0; j<n2; j++)
{
if(arr1[i]==arr2[j])
{
count++;
break;
}
}
}
if((count==n1)&&(count==n2))
{
printf("Arrays are same");
}
else
{
printf("Arrays are not same");
}
return 0;
}
import java.util.*;
import java.util.Scanner;
public class Main
{
public static boolean equal(int a1[],int a2[])
{
int n=a1.length;
int m=a2.length;
Arrays.sort(a1);
Arrays.sort(a2);
if (n!= m)
return false;
for (int i = 0; i < n; i++)
{
if (a1[i] != a2[i])
return false;
}
return true;
}
public static void main(String[] arg)
{
int[] a1 = new int[50];
int[] a2 = new int[50];
System.out.println("enter size of array 1");
Scanner sc = new Scanner(System.in);
int size1 = sc.nextInt();
System.out.println("enter element");
for(int i=0;i<size1;i++)
a1[i]=sc.nextInt();
System.out.println("enter size of array2");
int size2=sc.nextInt();
System.out.println("enter element");
for(int i=0;i<size2;i++)
a2[i]=sc.nextInt();
if(equal(a1,a2))
System.out.println("Both array is same");
else
System.out.println("arrays is not same");
}
}
Output
enter size of array 1
4
enter element
2
4
13
1
enter size of array2
4
enter element
4
2
1
13
Both array is same
import java.util.*;
public class Pavan
{
public static void main(String[] args)
{
int a[]={1,2,3,4};
int b[]={4,2,1,3};
Arrays.sort(a);
Arrays.sort(b);
if(Arrays.equals(a,b))
{
System.out.println(“both arrays are same”);
}
else
{
System.out.println(“not Same”);
}
}
}
Hey there,
Thanks for answering, Kindly join our Discord server for any technical related queries.
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int a[]={1,2,3,4};
int b[]={4,2,1,3};
Arrays.sort(a);
Arrays.sort(b);
if(Arrays.equals(a,b))
{
System.out.println(“both arrays are same”);
}
else
{
System.out.println(“not Same”);
}
}
}
Hey there,
Thanks for answering, Kindly join our Discord server for any technical related queries.