701742. Check Equal Arrays

Easy

Check if Two Arrays are Equal or Not

Slug: check-if-two-arrays-are-equal-or-not3847 Difficulty: Easy Id: 701742 Topic Tags: Arrays, Hash, Sorting, Data Structures, Algorithms Company Tags: Goldman Sachs

Summary

This problem involves comparing two arrays to determine if they are equal. The key concepts involved include arrays and data structures. The goal is to write an efficient algorithm that checks if the given arrays are identical or not.

Detailed Explanation

To solve this problem, we can use a simple approach of comparing each element in both arrays. Here's a step-by-step breakdown:

  1. Initialize two pointers, i and j, to start from the beginning of both arrays.
  2. Compare the elements at indices i and j. If they are equal, increment both pointers.
  3. If the elements are not equal or one array has ended before the other, return false (arrays are not equal).
  4. Continue this process until we reach the end of either array. If all elements match up to that point, return true (arrays are equal).

Here's a simple diagram to illustrate this approach:

  Array 1: [A, B, C]
  Array 2: [A, B, C]

    i        j
  +---+     +---+
  | A | --> | A |
  +---+     +---+
  | B | --> | B |
  +---+     +---+
  | C | --> | C |
  +---+     +---+

Time complexity: O(n), where n is the length of the arrays. We only need to iterate through each element once.

Space complexity: O(1), as we only use a few extra variables to store the pointers and don't require any additional data structures.

Optimized Solutions

Java

View on GeeksforGeeks
public boolean checkEqualArrays(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;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.