Program to check if two arrays are the same or not

06 May 2024


Description about the Program to check if two arrays are the same or not

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 to check if two arrays are the same 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(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 to check if two arrays are the same or not???????

#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 to check if two arrays are the same or not???????

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 to check if two arrays are the same or not???????

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

FAQ

Any Questions?
Look Here.

Related Articles

Program to identify if the character is an alphabet or not

Program to Replace substring in a string

Check if two strings match where one string contains wildcard characters

Lower Triangular Matrix

MATRIX MULTIPLICATION Program

Maximum Product Subarray

Minimum Scalar Product Program

Program for Binary to decimal conversion

Program for Binary to octal conversion

Program for Decimal to binary conversion

Program for Decimal to octal conversion

Program for Maximum Scalar Product

Program for Octal to binary conversion

Program for Octal to decimal conversion

Program To Add two matrices and print the resultant matrix

Program to Add two fractions

Program to calculate Maximum number of handshakes

Program to calculate the Frequency of characters in a string

Program to Capitalize the first and last letter of each word of a string

Program to change the given matrix to transpose of the matrix.

Program to check if String is a palindrome or not

Program to check if two strings are Anagram or not

Program to compare two strings

Program to concatenate a string

Program to copy a string

Program to Count common subsequence in two strings

Program to Count the sum of numbers in a string

Program to express a number as a sum of two prime numbers

Program to find Area of a rectangle

Program to find Area of a Triangle

Program to find ASCII values of a character

Program to find equilibrium index of an array

Program to find Factorial of a number

Program to find Fibonacci series up to n

Program to find Frequency of each element of an array

Program to find LCM of two numbers

Program to find longest palindrome in an array

Program to find minimum sum of absolute difference of given array

Program to find Number of days in a given month of a given year

Program to find Number of digits in an integer

Program to find number of even and odd elements in an array

Program to find number of integers which has exactly 9 divisors

Program to find Permutations in which n people can occupy r seats in a classroom.

Program to find Power of a number

Program to find roots of a quadratic equation

Program to find second smallest element in the array

Program to find smallest and largest element in an array

Program to find Sum of digits of a number

Program to find sum of elements in an array

Program to find Sum of N natural numbers

Program to find Sum of numbers in a given range

Program to find sum of positive square elements in the array

Program to find the Area of a circle

Program to find the array type

Program to find the double of the given number without using arithmetic operator

Program to find the Factors of a number

Program to find the Quadrants in which coordinates lie

Program to find whether an array is a subset of another array or not.

Program to find whether Arrays are disjoint or not

Program to find whether the numbers of an array be made equal

Program to identify if the character is a vowel or consonant

Program to identify if the number is Armstrong number or not

Program to identify if the number is even or odd

Program to identify if the number is Palindrome or not

Program to identify if the number is Perfect number or not

Program to identify if the number is Prime number or not

Program to identify if the number is Strong number or not

Program to identify of the a number is positive or negative

Program to print Armstrong numbers between two intervals

Program to print Diamond pattern printing using stars

Program to print Diamond pattern using Column number

Program to print Diamond pattern using Row numbers

Program to print Floyd’s triangle

Program to print hollow rectangle star pattern

Program to print Length of the string without using strlen() function

Program to print Non-repeating characters in a string

Program to print Palindromic pyramid pattern printing

Program to print Pascal triangle

Program to print prime numbers in a given range

Program to print Pyramid pattern using numbers

Program to print Pyramid pattern using stars

Program to print Solid rectangle star pattern

Program to print the length of a string

Program to print the max elements in all the rows.

Program to Remove brackets from an algebraic expression

Program to Remove characters in a string except alphabets

Program to remove duplicate elements in an array

Program to Remove spaces from a string

Program to Remove vowels from a string

Program to Replace all 0’s with 1 in a given integer

Program to reverse a given number

Program to reverse a string

Program to reverse the array

Program to sort the array

Program to Toggle each character in a string

Subtract matrix2 from matrix1 and print the resultant matrix program

Upper Triangular Matrix