array comparison
integer arrays
programming
algorithm
data structures

Compare two integer arrays with same length

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Comparing two integer arrays of the same length sounds simple, but the correct approach depends on what "compare" means in your problem. Sometimes you want exact element-by-element equality, sometimes lexical ordering, and sometimes a report of which positions differ.

Decide What Kind of Comparison You Need

Before writing code, choose one of these meanings:

  • exact equality in the same order
  • first-difference or mismatch detection
  • lexicographic ordering
  • equal contents regardless of order

Those are different operations. Arrays with the same length are only directly comparable element by element if order matters to you.

Exact Element-by-Element Equality

The most common case is checking whether both arrays have the same values in the same positions.

python
1def arrays_equal(a, b):
2    if len(a) != len(b):
3        return False
4
5    for i in range(len(a)):
6        if a[i] != b[i]:
7            return False
8    return True
9
10
11print(arrays_equal([1, 2, 3], [1, 2, 3]))
12print(arrays_equal([1, 2, 3], [1, 3, 2]))

Since your arrays are already known to have the same length, the key step is the element comparison inside the loop.

In Python, the built-in list comparison is even simpler:

python
print([1, 2, 3] == [1, 2, 3])
print([1, 2, 3] == [1, 3, 2])

But writing the loop yourself is still useful when you want more control or more detailed diagnostics.

Find the First Mismatch

Sometimes the important question is not "are they equal" but "where do they first differ."

python
1def first_mismatch(a, b):
2    for i in range(len(a)):
3        if a[i] != b[i]:
4            return i, a[i], b[i]
5    return None
6
7
8print(first_mismatch([4, 7, 9], [4, 8, 9]))
9print(first_mismatch([4, 7, 9], [4, 7, 9]))

This is useful in testing, log analysis, and validation pipelines where you need an actionable failure location rather than just True or False.

Lexicographic Comparison

If you want to know whether one array comes before another in ordered sequence comparison, use lexicographic logic.

python
1def compare_lexicographically(a, b):
2    for i in range(len(a)):
3        if a[i] < b[i]:
4            return -1
5        if a[i] > b[i]:
6            return 1
7    return 0
8
9
10print(compare_lexicographically([1, 2, 3], [1, 2, 4]))
11print(compare_lexicographically([1, 5, 3], [1, 2, 9]))
12print(compare_lexicographically([1, 2, 3], [1, 2, 3]))

This is not the same as checking equality. It is the array version of dictionary order.

Equal Contents Regardless of Order

If order does not matter, then element-by-element comparison is the wrong tool. Sort or count frequencies instead.

python
1from collections import Counter
2
3
4def same_multiset(a, b):
5    return Counter(a) == Counter(b)
6
7
8print(same_multiset([1, 2, 2, 3], [2, 1, 3, 2]))
9print(same_multiset([1, 2, 2], [1, 1, 2]))

This compares the values and their counts, ignoring position.

Efficient Comparison with NumPy

If the arrays are NumPy arrays, use vectorized comparisons:

python
1import numpy as np
2
3a = np.array([1, 2, 3])
4b = np.array([1, 2, 3])
5c = np.array([1, 3, 2])
6
7print(np.array_equal(a, b))
8print(np.array_equal(a, c))
9print(a == c)

np.array_equal is the clear choice when you want a single Boolean for exact shape-and-value equality.

Practical Guidance

A reliable rule is:

  • use direct equality for exact same-order comparison
  • use a first-mismatch function for debugging
  • use lexicographic logic for ordering
  • use sorting or frequency counts when order is irrelevant

Choosing the wrong comparison rule is a more common bug than writing the comparison loop incorrectly.

Common Pitfalls

The biggest mistake is assuming arrays with the same values are equal even when the elements are in different positions. Arrays are usually order-sensitive.

Another issue is using lexicographic comparison when you really wanted equality or mismatch reporting. Those operations answer different questions.

A third problem is forcing manual loops on NumPy arrays when a library helper such as np.array_equal communicates intent more clearly.

Summary

  • "Compare" can mean equality, ordering, mismatch detection, or same contents ignoring order.
  • For exact equality, compare each position or use the language's built-in array equality.
  • For debugging, return the first mismatching index and values.
  • For order-insensitive comparison, compare sorted arrays or frequency counts.
  • Pick the comparison rule that matches the problem instead of assuming one universal definition.

Course illustration
Course illustration

All Rights Reserved.