Sort an array according to the order defined by another array
Last Updated :
08 Aug, 2025
Given two arrays a1[] and a2[], sort a1[] such that elements appear in the order of a2[]. Elements not in a2[] should be placed at the end in ascending order.
Example:
Input: a1 = [2, 1, 2, 3, 4], a2 = [2, 1, 2]
Output: [2, 2, 1, 3, 4]
Explanation: Elements 2 and 1 follow the order in a2. Remaining 3 and 4 are sorted at the end.
Input: a1 = [4, 1, 3, 3, 2], a2 = [3, 1]
Output: [3, 3, 1, 2, 4]
Explanation: Elements 3 and 1 come first as per a2. Others (2, 4) are sorted and placed after.
[Approach 1] Using Hashing - O(m × log m + n) Time and O(m) Space
The main idea is to use a hash map to count frequencies of elements in a1, so we can efficiently place elements in the desired order. Then, iterate through a2 to append matching elements in order, followed by the sorted remaining elements from a1. This ensures correct relative order from a2, and sorted placement of extras.
Step-By-Step Approach:
- Count frequency of each element in a1 using a hash map.
- Add elements from a2 to the result based on their frequency.
- Remove those elements from the map after processing.
- Sort and add the remaining elements to the result using their frequency.
C++
#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std;
void relativeSort(vector<int>& a1, vector<int>& a2){
int m = a1.size(), n = a2.size();
unordered_map<int, int> freq;
// Count frequency of each element in a1
for (int i = 0; i < m; i++) {
freq[a1[i]]++;
}
int index = 0;
// Place elements of a2 in a1 based on frequency
for (int i = 0; i < n; i++) {
while (freq[a2[i]]) {
a1[index++] = a2[i];
freq[a2[i]]--;
}
freq.erase(a2[i]);
}
// Collect remaining elements and sort them
vector<int> remaining;
for (auto& pair : freq) {
while (pair.second--) {
remaining.push_back(pair.first);
}
}
sort(remaining.begin(), remaining.end());
// Append remaining elements to a1
for (int i : remaining) {
a1[index++] = i;
}
}
int main(){
vector<int> a1 = { 2, 1, 2, 3, 4};
vector<int> a2 = { 2, 1, 2 };
relativeSort(a1, a2);
for (int i = 0; i < a1.size(); i++) {
cout << a1[i] << " ";
}
cout << endl;
return 0;
}
Java
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class GfG {
static void relativeSort(int[] a1, int[] a2) {
int m = a1.length, n = a2.length;
Map<Integer, Integer> freq = new HashMap<>();
// Count frequency of each element in a1
for (int i = 0; i < m; i++) {
freq.put(a1[i], freq.getOrDefault(a1[i], 0) + 1);
}
int index = 0;
// Place elements of a2 in a1 based on frequency
for (int i = 0; i < n; i++) {
while (freq.getOrDefault(a2[i], 0) > 0) {
a1[index++] = a2[i];
freq.put(a2[i], freq.get(a2[i]) - 1);
}
freq.remove(a2[i]);
}
// Collect remaining elements and sort them
List<Integer> remaining = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
for (int i = 0; i < entry.getValue(); i++) {
remaining.add(entry.getKey());
}
}
Collections.sort(remaining);
// Append remaining elements to a1
for (int num : remaining) {
a1[index++] = num;
}
}
public static void main(String[] args) {
int[] a1 = {2, 1, 2, 3, 4};
int[] a2 = {2, 1, 2};
relativeSort(a1, a2);
for (int num : a1) {
System.out.print(num + " ");
}
System.out.println();
}
}
Python
def relativeSort(a1, a2):
m, n = len(a1), len(a2)
freq = {}
# Count frequency of each element in a1
for num in a1:
freq[num] = freq.get(num, 0) + 1
index = 0
# Place elements of a2 in a1 based on frequency
for num in a2:
while freq.get(num, 0) > 0:
a1[index] = num
index += 1
freq[num] -= 1
freq.pop(num, None)
# Collect remaining elements and sort them
remaining = []
for key in freq:
remaining.extend([key] * freq[key])
remaining.sort()
# Append remaining elements to a1
for num in remaining:
a1[index] = num
index += 1
if __name__ == "__main__":
a1 = [2, 1, 2, 3, 4]
a2 = [2, 1, 2]
relativeSort(a1, a2)
for num in a1:
print(num, end=" ")
print()
C#
using System;
using System.Collections.Generic;
class GfG {
static void relativeSort(int[] a1, int[] a2) {
int m = a1.Length, n = a2.Length;
Dictionary<int, int> freq = new Dictionary<int, int>();
// Count frequency of each element in a1
for (int i = 0; i < m; i++) {
if (!freq.ContainsKey(a1[i]))
freq[a1[i]] = 0;
freq[a1[i]]++;
}
int index = 0;
// Place elements of a2 in a1 based on frequency
for (int i = 0; i < n; i++) {
while (freq.ContainsKey(a2[i]) && freq[a2[i]] > 0) {
a1[index++] = a2[i];
freq[a2[i]]--;
}
freq.Remove(a2[i]);
}
// Collect remaining elements and sort them
List<int> remaining = new List<int>();
foreach (var pair in freq) {
for (int i = 0; i < pair.Value; i++) {
remaining.Add(pair.Key);
}
}
remaining.Sort();
// Append remaining elements to a1
foreach (int num in remaining) {
a1[index++] = num;
}
}
static void Main() {
int[] a1 = {2, 1, 2, 3, 4};
int[] a2 = {2, 1, 2};
relativeSort(a1, a2);
foreach (int num in a1) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
JavaScript
function relativeSort(a1, a2) {
let m = a1.length, n = a2.length;
let freq = new Map();
// Count frequency of each element in a1
for (let i = 0; i < m; i++) {
freq.set(a1[i], (freq.get(a1[i]) || 0) + 1);
}
let index = 0;
// Place elements of a2 in a1 based on frequency
for (let i = 0; i < n; i++) {
while (freq.get(a2[i]) > 0) {
a1[index++] = a2[i];
freq.set(a2[i], freq.get(a2[i]) - 1);
}
freq.delete(a2[i]);
}
// Collect remaining elements and sort them
let remaining = [];
for (let [key, val] of freq.entries()) {
while (val--) remaining.push(key);
}
remaining.sort((a, b) => a - b);
// Append remaining elements to a1
for (let i = 0; i < remaining.length; i++) {
a1[index++] = remaining[i];
}
}
// Driver Code
let a1 = [2, 1, 2, 3, 4];
let a2 = [2, 1, 2];
relativeSort(a1, a2);
console.log(a1.join(" "));
The idea is to sort the first array a1[] based on the relative order of elements defined in the second array a2[]. A hash map stores the index of each element in a2[] to define their priority. While sorting a1[], a custom comparator uses this map to ensure that elements present in a2[] come first in the correct order, and the rest are sorted naturally in increasing order.
Step-By-Step Approach:
- Traverse a2[] and store the index of each element in a hash map to define custom order.
- Sort a1[] using a lambda comparator with the help of the map.
- If both elements are in the map, compare their positions from the map.
- If only one is in the map, prioritize it over the one that is not.
- If neither is in the map, sort them in increasing numerical order.
- The final a1[] follows the order of a2[] for common elements and sorts the rest normally.
C++
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
void relativeSort(vector<int>& a1, vector<int>& a2) {
// create a map to store the index of
// each element in a2
unordered_map<int, int> mp;
for (int i = 0; i < a2.size(); i++) {
if (mp.count(a2[i]) == 0)
mp[a2[i]] = i;
}
// custom comparator using lambda
sort(a1.begin(), a1.end(), [&](int a, int b) {
bool aIn = mp.count(a), bIn = mp.count(b);
if (!aIn && !bIn) return a < b;
if (!aIn) return false;
if (!bIn) return true;
return mp[a] < mp[b];
});
}
int main() {
vector<int> a1 = {2, 1, 2, 3, 4};
vector<int> a2 = {2, 1, 2};
relativeSort(a1, a2);
for (int x : a1)
cout << x << " ";
cout << endl;
return 0;
}
Java
import java.util.Map;
import java.util.HashMap;
import java.util.Arrays;
class GfG {
static void relativeSort(int[] a1, int[] a2) {
// Create a map to store the index of each
// element in a2
Map<Integer, Integer> mp = new HashMap<>();
for (int i = 0; i < a2.length; i++) {
if (!mp.containsKey(a2[i]))
mp.put(a2[i], i);
}
// Convert int[] to Integer[] for custom sorting
Integer[] temp = Arrays.stream(a1).boxed().toArray(Integer[]::new);
// Custom comparator using lambda
Arrays.sort(temp, (a, b) -> {
boolean aIn = mp.containsKey(a), bIn = mp.containsKey(b);
if (!aIn && !bIn) return Integer.compare(a, b);
if (!aIn) return 1;
if (!bIn) return -1;
return Integer.compare(mp.get(a), mp.get(b));
});
// Copy sorted values back to original array
for (int i = 0; i < a1.length; i++) {
a1[i] = temp[i];
}
}
public static void main(String[] args) {
int[] a1 = {2, 1, 2, 3, 4};
int[] a2 = {2, 1, 2};
relativeSort(a1, a2);
for (int num : a1)
System.out.print(num + " ");
System.out.println();
}
}
Python
def relativeSort(a1, a2):
# create a map to store the index of each element in a2
mp = {}
for i in range(len(a2)):
if a2[i] not in mp:
mp[a2[i]] = i
# custom comparator using lambda inside sort's key
a1.sort(key=lambda x: (mp.get(x, float('inf')), x))
if __name__ == "__main__":
a1 = [2, 1, 2, 3, 4]
a2 = [2, 1, 2]
relativeSort(a1, a2)
for x in a1:
print(x, end=" ")
print()
C#
using System;
using System.Collections.Generic;
class GfG {
static void relativeSort(int[] a1, int[] a2) {
// Create a map to store the index of each element in a2
Dictionary<int, int> mp = new Dictionary<int, int>();
for (int i = 0; i < a2.Length; i++) {
if (!mp.ContainsKey(a2[i]))
mp[a2[i]] = i;
}
// Custom comparator using lambda
Array.Sort(a1, (a, b) => {
bool aIn = mp.ContainsKey(a), bIn = mp.ContainsKey(b);
if (!aIn && !bIn) return a.CompareTo(b);
if (!aIn) return 1;
if (!bIn) return -1;
return mp[a].CompareTo(mp[b]);
});
}
static void Main() {
int[] a1 = {2, 1, 2, 3, 4};
int[] a2 = {2, 1, 2};
relativeSort(a1, a2);
foreach (int x in a1)
Console.Write(x + " ");
Console.WriteLine();
}
}
JavaScript
function relativeSort(a1, a2) {
// Create a map to store the index of
// each element in a2
let mp = new Map();
for (let i = 0; i < a2.length; i++) {
if (!mp.has(a2[i])) {
mp.set(a2[i], i);
}
}
// Custom comparator using lambda
a1.sort((a, b) => {
let aIn = mp.has(a), bIn = mp.has(b);
if (!aIn && !bIn) return a - b;
if (!aIn) return 1;
if (!bIn) return -1;
return mp.get(a) - mp.get(b);
});
}
// Driver Code
let a1 = [2, 1, 2, 3, 4];
let a2 = [2, 1, 2];
relativeSort(a1, a2);
console.log(a1.join(" "));
Time Complexity: O(m log m + n), sorting a1 takes O(m log m) time, and building the map from a2 takes O(n) time, where m is the size of a1 and n is the size of a2.
Auxiliary Space: O(n), an unordered map is used to store positions of m elements from a2, contributing to the extra space.
Sort an array according to the other | DSA Problem
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem