Check if array B is a permutation of A
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To check whether array B is a permutation of array A, you need to verify that both arrays contain exactly the same elements with exactly the same frequencies. The order does not matter, but length and counts do.
The Core Definition
Two arrays are permutations of each other if:
- they have the same length
- every distinct value appears the same number of times in both arrays
That means [3, 1, 4, 2] and [4, 2, 1, 3] are permutations, but [1, 2, 2] and [1, 1, 2] are not because the frequencies differ.
The shortest useful mental model is: permutation checking is a multiset-equality problem.
Method 1: Sort Both Arrays
The simplest general solution is to sort both arrays and compare them element by element.
This is easy to write and easy to trust. Its time complexity is O(n log n) because sorting dominates the work.
For many small and medium-sized datasets, that is perfectly acceptable.
Method 2: Count Frequencies With a Hash Map
If you want linear time, count how often each value appears.
This runs in O(n) expected time with O(n) extra space. It is usually the best answer when the arrays are not already sorted and you care more about speed than minimal memory.
The same idea can be written manually without Counter:
That version makes the underlying logic explicit.
Method 3: Counting Array for Small Integer Ranges
If the arrays contain integers in a small known range, you can replace the hash map with a counting array.
This is very fast, but it only fits problems where the domain is constrained and non-negative.
How to Choose Among the Methods
A practical guide is:
- use sorting when clarity is more important than absolute performance
- use a frequency map for general linear-time checking
- use a counting array only when the value range is small and known
The correct choice depends on the data model, not just on asymptotic complexity.
If the arrays are already sorted, comparison is even simpler because no extra work is needed beyond a direct scan.
Common Pitfalls
A common mistake is checking only whether both arrays contain the same distinct values. Frequency matters too, so sets alone are not enough.
Another mistake is forgetting the length check. If the lengths differ, the arrays cannot be permutations.
People also often write a frequency decrement loop but forget to reject negative counts immediately. That can hide mismatches until later.
Finally, be clear about element types. If the arrays contain complex objects, permutation checking depends on the equality semantics of those objects, not just on their printed appearance.
Summary
- Two arrays are permutations of each other when they have the same length and the same value frequencies.
- Sorting both arrays is the simplest general solution.
- A frequency map gives expected
O(n)time for general data. - A counting array is a fast special-case optimization for small integer ranges.
- Do not use sets alone when duplicates matter.

