How to rearrange an array by indices array?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Rearranging an array by an indices array means applying a permutation. Usually, the rule is that indices[i] tells you the destination position of arr[i]. The simplest solution uses a second array, while the more interesting version applies the permutation in place by following cycles.
Clarify the Meaning of the Indices Array
Suppose:
- data array is
['A', 'B', 'C', 'D'] - indices array is
[2, 0, 1, 3]
That means:
- '
'A'moves to index2' - '
'B'moves to index0' - '
'C'moves to index1' - '
'D'stays at index3'
So the final array is:
This interpretation matters because some problems define the index array in the opposite direction. Always verify whether the indices represent source positions or destination positions.
The Easiest Solution: Use an Extra Array
The clearest approach is to build a result array of the same size and place each element into its target position.
This runs in O(n) time and uses O(n) extra space. For most applications, it is the best balance of simplicity and reliability.
Validate the Indices First
A valid permutation of length n must contain every value from 0 through n - 1 exactly once. If the indices array contains duplicates or out-of-range values, the rearrangement is invalid.
This check is useful because otherwise elements may be overwritten or positions may never be filled.
In-Place Rearrangement With Cycles
If extra memory matters, you can apply the permutation in place. The idea is to follow cycles in the mapping and swap until each element reaches its final position.
Notice that this mutates both arr and indices. That is common in in-place solutions because the index array itself is used as part of the bookkeeping.
Why the In-Place Version Works
A permutation is made of cycles. In the example [2, 0, 1, 3], the first three positions form one cycle:
- '
0 -> 2' - '
2 -> 1' - '
1 -> 0'
The in-place algorithm keeps swapping until each cycle collapses into the identity mapping, meaning indices[i] == i for every position.
That is why the loop condition checks whether the current element is already in its final location.
When to Choose Which Approach
Use the extra-array solution when:
- clarity matters most
- memory is not tight
- you want to keep the original indices unchanged
Use the in-place solution when:
- auxiliary memory matters
- mutating the index array is acceptable
- the permutation property is guaranteed or validated
In production code, the extra-array version is often preferable because it is much easier to read and debug.
Common Pitfalls
The biggest mistake is misunderstanding what indices[i] means. If the problem defines it as a source position and you treat it as a destination, the result will be wrong even though the code looks reasonable.
Another mistake is skipping validation. Duplicate or out-of-range indices make the permutation invalid and can silently corrupt the output.
People also forget that the in-place algorithm usually mutates the indices array. If the caller needs to keep the original indices, pass a copy.
Finally, do not use the in-place version just because it looks clever. If extra space is acceptable, the direct construction approach is often better.
Summary
- Rearranging by an indices array is an application of a permutation.
- The simplest solution builds a new array and places each element in its target position.
- A valid indices array must be a true permutation of
0throughn - 1. - In-place rearrangement works by following permutation cycles.
- Choose the simpler extra-array version unless memory constraints really justify the in-place approach.
Related reading
- How to rearrange data in array so that two similar items are not next to each other?
- How to recursively list all the files in a directory in C?
- How to Reduce Time Complexity
- How to remove convexity defects in a Sudoku square?
- How to remove a key-value pair from swift dictionary?
- How to remove a key from HashMap while iterating over it?
- How to remove elements from a binary heap?
- How to remove elements from a vector by order of priority

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.