Arrays January 12 ,2026

Check if Two Arrays Are Equal

Problem Statement

Given two arrays, check if they are equal. Two arrays are considered equal if:

  1. They have the same size.
  2. All elements of one array are present in the other array, including frequency.
  3. The order of elements may or may not matter depending on the variation.

👉 In this problem, we consider arrays equal regardless of order (most common interview variation).

Example 1

Input:

arr1 = [1, 2, 3, 4]
arr2 = [4, 3, 2, 1]

Output:

True

Explanation:

  • Both arrays have the same elements and frequency.
  • Order does not matter.

Example 2

Input:

arr1 = [1, 2, 2, 3]
arr2 = [2, 1, 3, 2]

Output:

True

Explanation:

  • Elements and frequency match.
  • Arrays are equal.

Example 3

Input:

arr1 = [1, 2, 3]
arr2 = [1, 2, 4]

Output:

False

Explanation:

  • Element 3 in arr1 does not match element 4 in arr2.

Why This Problem Is Important

Checking array equality is a common task in:

  • Data comparison
  • Test validations
  • Array manipulation problems
  • Interview coding questions

It helps you understand:

  • Frequency counting
  • Sorting techniques
  • Hashing for fast lookup

Approaches to Solve the Problem

  1. Sort and Compare
  2. Using Hash Maps / Frequency Counting (Efficient)

Approach 1: Sort and Compare

Idea

  1. Sort both arrays.
  2. Compare elements one by one.
  3. If any element differs → arrays are not equal.

Time & Space Complexity

  • Time Complexity: O(n log n) (due to sorting)
  • Space Complexity: O(1) or O(n) depending on sorting method

Implementations

C Implementation

#include 
#include 

int compare(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

int main() {
    int arr1[] = {1, 2, 2, 3};
    int arr2[] = {2, 1, 3, 2};
    int n = 4, m = 4;

    if (n != m) {
        printf("Arrays are not equal");
        return 0;
    }

    qsort(arr1, n, sizeof(int), compare);
    qsort(arr2, m, sizeof(int), compare);

    for (int i = 0; i < n; i++) {
        if (arr1[i] != arr2[i]) {
            printf("Arrays are not equal");
            return 0;
        }
    }

    printf("Arrays are equal");
    return 0;
}

C++ Implementation

#include 
#include 
#include 
using namespace std;

int main() {
    vector arr1 = {1, 2, 2, 3};
    vector arr2 = {2, 1, 3, 2};

    if (arr1.size() != arr2.size()) {
        cout << "Arrays are not equal";
        return 0;
    }

    sort(arr1.begin(), arr1.end());
    sort(arr2.begin(), arr2.end());

    cout << (arr1 == arr2 ? "Arrays are equal" : "Arrays are not equal");
    return 0;
}

Java Implementation

import java.util.*;

public class Main {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 2, 3};
        int[] arr2 = {2, 1, 3, 2};

        if (arr1.length != arr2.length) {
            System.out.println("Arrays are not equal");
            return;
        }

        Arrays.sort(arr1);
        Arrays.sort(arr2);

        System.out.println(
            Arrays.equals(arr1, arr2)
            ? "Arrays are equal"
            : "Arrays are not equal"
        );
    }
}

Python Implementation

arr1 = [1, 2, 2, 3]
arr2 = [2, 1, 3, 2]

if sorted(arr1) == sorted(arr2):
    print("Arrays are equal")
else:
    print("Arrays are not equal")

C# Implementation

using System;

class Program {
    static void Main() {
        int[] arr1 = {1, 2, 2, 3};
        int[] arr2 = {2, 1, 3, 2};

        if (arr1.Length != arr2.Length) {
            Console.WriteLine("Arrays are not equal");
            return;
        }

        Array.Sort(arr1);
        Array.Sort(arr2);

        Console.WriteLine(
            arr1.SequenceEqual(arr2)
            ? "Arrays are equal"
            : "Arrays are not equal"
        );
    }
}

JavaScript Implementation

let arr1 = [1, 2, 2, 3];
let arr2 = [2, 1, 3, 2];

arr1.sort((a,b) => a - b);
arr2.sort((a,b) => a - b);

console.log(
  JSON.stringify(arr1) === JSON.stringify(arr2)
  ? "Arrays are equal"
  : "Arrays are not equal"
);

Approach 2: Using Hash Maps (Efficient)

Idea

  1. Create a frequency map for the first array.
  2. Decrease counts while traversing the second array.
  3. If any element is missing or count mismatches → arrays are not equal.

Time & Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(n)

✅ Recommended for large arrays or interview scenarios.
 

Implementations

C Implementation

#include 
#include 

#define MAX 100000

int main() {
    int arr1[] = {1, 2, 2, 3};
    int arr2[] = {2, 1, 3, 2};
    int n = 4, m = 4;

    if (n != m) {
        printf("Arrays are not equal");
        return 0;
    }

    int freq[MAX] = {0};

    for (int i = 0; i < n; i++)
        freq[arr1[i]]++;

    for (int i = 0; i < n; i++) {
        if (freq[arr2[i]] == 0) {
            printf("Arrays are not equal");
            return 0;
        }
        freq[arr2[i]]--;
    }

    printf("Arrays are equal");
    return 0;
}

C++ Implementation

#include 
#include 
#include 
using namespace std;

int main() {
    vector arr1 = {1, 2, 2, 3};
    vector arr2 = {2, 1, 3, 2};

    if (arr1.size() != arr2.size()) {
        cout << "Arrays are not equal";
        return 0;
    }

    unordered_map freq;

    for (int num : arr1)
        freq[num]++;

    for (int num : arr2) {
        if (freq[num] == 0) {
            cout << "Arrays are not equal";
            return 0;
        }
        freq[num]--;
    }

    cout << "Arrays are equal";
    return 0;
}

Java Implementation

import java.util.*;

public class Main {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 2, 3};
        int[] arr2 = {2, 1, 3, 2};

        if (arr1.length != arr2.length) {
            System.out.println("Arrays are not equal");
            return;
        }

        HashMap map = new HashMap<>();

        for (int num : arr1)
            map.put(num, map.getOrDefault(num, 0) + 1);

        for (int num : arr2) {
            if (!map.containsKey(num)) {
                System.out.println("Arrays are not equal");
                return;
            }
            map.put(num, map.get(num) - 1);
            if (map.get(num) == 0)
                map.remove(num);
        }

        System.out.println("Arrays are equal");
    }
}

Python Implementation

arr1 = [1, 2, 2, 3]
arr2 = [2, 1, 3, 2]

from collections import Counter

if Counter(arr1) == Counter(arr2):
    print("Arrays are equal")
else:
    print("Arrays are not equal")

C# Implementation

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        int[] arr1 = {1, 2, 2, 3};
        int[] arr2 = {2, 1, 3, 2};

        if (arr1.Length != arr2.Length) {
            Console.WriteLine("Arrays are not equal");
            return;
        }

        Dictionary freq = new Dictionary();

        foreach (int num in arr1)
            freq[num] = freq.GetValueOrDefault(num, 0) + 1;

        foreach (int num in arr2) {
            if (!freq.ContainsKey(num)) {
                Console.WriteLine("Arrays are not equal");
                return;
            }
            freq[num]--;
            if (freq[num] == 0)
                freq.Remove(num);
        }

        Console.WriteLine("Arrays are equal");
    }
}

JavaScript Implementation

let arr1 = [1, 2, 2, 3];
let arr2 = [2, 1, 3, 2];

if (arr1.length !== arr2.length) {
    console.log("Arrays are not equal");
} else {
    let map = new Map();

    for (let num of arr1)
        map.set(num, (map.get(num) || 0) + 1);

    let equal = true;
    for (let num of arr2) {
        if (!map.has(num)) {
            equal = false;
            break;
        }
        map.set(num, map.get(num) - 1);
        if (map.get(num) === 0)
            map.delete(num);
    }

    console.log(equal ? "Arrays are equal" : "Arrays are not equal");
}

 

Dry Run (Step-by-Step)

Steparr1arr2Notes
Initial[1,2,2,3][2,1,3,2]Input arrays
Sorting[1,2,2,3][1,2,2,3]Arrays sorted
CompareElement by element All match → Equal
Using Hash MapCount frequencySubtract while traversing arr2Map empty at end → Equal

Summary

Checking if two arrays are equal is a fundamental interview question.

  • Sorting is simple and works well for smaller arrays.
  • Hash Map / Frequency count is efficient for large arrays and avoids O(n log n) sorting overhead.

Mastering this problem prepares you for array comparison, frequency counting, and multi-set operations.

Next Problem in the Series

Merge Two Sorted Arrays

Sanjiv
0

You must logged in to post comments.

Related Blogs

Find the S...
Arrays February 02 ,2026

Find the Second Smal...

Find the S...
Arrays January 01 ,2026

Find the Sum of All...

Find the M...
Arrays January 01 ,2026

Find the Maximum Ele...

Find the M...
Arrays January 01 ,2026

Find the Minimum Ele...

Count Even...
Arrays January 01 ,2026

Count Even and Odd N...

Search an...
Arrays January 01 ,2026

Search an Element in...

Copy One A...
Arrays January 01 ,2026

Copy One Array into...

Reverse an...
Arrays January 01 ,2026

Reverse an Array

Print Alte...
Arrays January 01 ,2026

Print Alternate Elem...

Find the L...
Arrays January 01 ,2026

Find the Length of a...

Check if a...
Arrays January 01 ,2026

Check if an Array is...

Find the F...
Arrays January 01 ,2026

Find the First Eleme...

Find the L...
Arrays January 01 ,2026

Find the Last Elemen...

Count the...
Arrays January 01 ,2026

Count the Number of...

Replace Al...
Arrays January 01 ,2026

Replace All Elements...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at E...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at O...

Find the A...
Arrays January 01 ,2026

Find the Average of...

Count the...
Arrays January 01 ,2026

Count the Number of...

Remove Dup...
Arrays January 01 ,2026

Remove Duplicate Ele...

Move All Z...
Arrays January 01 ,2026

Move All Zeros to th...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by K...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by O...

Merge Two...
Arrays January 01 ,2026

Merge Two Sorted Arr...

Find Missi...
Arrays January 01 ,2026

Find Missing Number...

Find Dupli...
Arrays January 01 ,2026

Find Duplicate Eleme...

Count Freq...
Arrays January 01 ,2026

Count Frequency of E...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All U...
Arrays January 01 ,2026

Find All Unique Elem...

Insert an...
Arrays January 01 ,2026

Insert an Element at...

Delete an...
Arrays January 01 ,2026

Delete an Element fr...

Find the I...
Arrays January 01 ,2026

Find the Index of an...

Find Union...
Arrays January 01 ,2026

Find Union of Two Ar...

Find Inter...
Arrays January 01 ,2026

Find Intersection of...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s...

Find the L...
Arrays January 01 ,2026

Find the Largest Sum...

Kadane’s A...
Arrays January 01 ,2026

Kadane’s Algorithm (...

Two Sum Pr...
Arrays January 01 ,2026

Two Sum Problem

Subarray w...
Arrays January 01 ,2026

Subarray with Given...

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Rearrange...
Arrays January 01 ,2026

Rearrange Array Alte...

Leaders in...
Arrays January 01 ,2026

Leaders in an Array

Equilibriu...
Arrays January 01 ,2026

Equilibrium Index of...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s,...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All P...
Arrays January 01 ,2026

Find All Pairs with...

Longest Co...
Arrays January 01 ,2026

Longest Consecutive...

Product of...
Arrays January 01 ,2026

Product of Array Exc...

Maximum Pr...
Arrays January 01 ,2026

Maximum Product Suba...

Find the F...
Arrays January 01 ,2026

Find the First Missi...

Count Inve...
Arrays January 01 ,2026

Count Inversions in...

Rearrange...
Arrays January 01 ,2026

Rearrange Array by S...

Check if A...
Arrays January 01 ,2026

Check if Array Can B...

Trapping R...
Arrays January 01 ,2026

Trapping Rain Water

Find Minim...
Arrays January 01 ,2026

Find Minimum in Rota...

Search in...
Arrays January 01 ,2026

Search in Rotated So...

Median of...
Arrays January 01 ,2026

Median of Two Sorted...

Merge Inte...
Arrays January 01 ,2026

Merge Intervals

Count Reve...
Arrays January 01 ,2026

Count Reverse Pairs

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Largest Re...
Arrays January 01 ,2026

Largest Rectangle in...

Maximum Su...
Arrays January 01 ,2026

Maximum Sum Rectangl...

Subarray S...
Arrays January 01 ,2026

Subarray Sum Equals...

Count Dist...
Arrays January 01 ,2026

Count Distinct Eleme...

Sliding Wi...
Arrays January 01 ,2026

Sliding Window Maxim...

Find K Max...
Arrays January 01 ,2026

Find K Maximum Eleme...

Minimum Nu...
Arrays January 01 ,2026

Minimum Number of Ju...

Chocolate...
Arrays January 01 ,2026

Chocolate Distributi...

Find All T...
Arrays January 01 ,2026

Find All Triplets Wi...

Kth Smalle...
Arrays January 01 ,2026

Kth Smallest Element...

Maximum Le...
Arrays January 01 ,2026

Maximum Length Biton...

Find the S...
Arrays February 02 ,2026

Find the Second Larg...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com