Program to Compare Two Arrays

Program to Compare Two Arrays

  • Write a program to Compare Two Arrays in C
  • Write a program to Compare Two Arrays in C++
  • Write a program to Compare Two Arrays in Python
  • Write a program to Compare Two Arrays in PHP
  • Write a program to Compare Two Arrays in Java
  • Write a program to Compare Two Arrays in Java Script
  • Write a program to Compare Two Arrays in C#

Explanation:

Comparing two arrays involves checking if they are equal or different based on some criteria. The most common comparison types are:

  1. Equality:
    • Arrays are considered equal if:
      • They have the same length.
      • They contain the same elements in the same order.
  2. Subset:
    • One array is a subset of another if all its elements exist in the other array (not necessarily in the same order).

Steps:

  1. Check if the lengths of the two arrays are the same.
  2. If the lengths are different, the arrays are not equal.
  3. If the lengths are the same:
    • Iterate through the arrays and compare elements at the same indices.
    • If any mismatch is found, the arrays are not equal.
  4. If no mismatches are found, the arrays are equal.

Program to Compare Two Arrays

#include <stdio.h>
#include <stdbool.h>

bool compareArrays(int arr1[], int arr2[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr1[i] != arr2[i]) {
            return false;
        }
    }
    return true;
}

int main() {
    int arr1[] = {1, 2, 3, 4, 5};
    int arr2[] = {1, 2, 3, 4, 5};  // Change this to test different arrays
    int size = sizeof(arr1) / sizeof(arr1[0]);

    if (compareArrays(arr1, arr2, size)) {
        printf("Arrays are identical.\n");
    } else {
        printf("Arrays are not identical.\n");
    }

    return 0;
}

#include <iostream>
using namespace std;

bool compareArrays(int arr1[], int arr2[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr1[i] != arr2[i]) {
            return false;
        }
    }
    return true;
}

int main() {
    int arr1[] = {1, 2, 3, 4, 5};
    int arr2[] = {1, 2, 3, 4, 5};  // Change this to test different arrays
    int size = sizeof(arr1) / sizeof(arr1[0]);

    if (compareArrays(arr1, arr2, size)) {
        cout << "Arrays are identical." << endl;
    } else {
        cout << "Arrays are not identical." << endl;
    }

    return 0;
}

def compare_arrays(arr1, arr2):
    return arr1 == arr2

arr1 = [1, 2, 3, 4, 5]
arr2 = [1, 2, 3, 4, 5]  # Change this to test different arrays

if compare_arrays(arr1, arr2):
    print("Arrays are identical.")
else:
    print("Arrays are not identical.")

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><?php
function compareArrays($arr1, $arr2) {
    return $arr1 === $arr2;
}

$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(1, 2, 3, 4, 5);  // Change this to test different arrays

if (compareArrays($arr1, $arr2)) {
    echo "Arrays are identical.\n";
} else {
    echo "Arrays are not identical.\n";
}
?>

public class Main {
    public static boolean compareArrays(int[] arr1, int[] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }

        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5};
        int[] arr2 = {1, 2, 3, 4, 5};  // Change this to test different arrays

        if (compareArrays(arr1, arr2)) {
            System.out.println("Arrays are identical.");
        } else {
            System.out.println("Arrays are not identical.");
        }
    }
}

function compareArrays(arr1, arr2) {
    return arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]);
}

const arr1 = [1, 2, 3, 4, 5];
const arr2 = [1, 2, 3, 4, 5];  // Change this to test different arrays

if (compareArrays(arr1, arr2)) {
    console.log("Arrays are identical.");
} else {
    console.log("Arrays are not identical.");
}

using System;

class Program {
    static bool CompareArrays(int[] arr1, int[] arr2) {
        if (arr1.Length != arr2.Length) {
            return false;
        }

        for (int i = 0; i < arr1.Length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }

        return true;
    }

    static void Main() {
        int[] arr1 = {1, 2, 3, 4, 5};
        int[] arr2 = {1, 2, 3, 4, 5};  // Change this to test different arrays

        if (CompareArrays(arr1, arr2)) {
            Console.WriteLine("Arrays are identical.");
        } else {
            Console.WriteLine("Arrays are not identical.");
        }
    }
}

List of All Programs