#3668
Easy Algorithms Restore finishing order
Array Hash Table
91.1% acceptance
Feb 25, 2026
110
4
You are given an integer array order of length n and an integer array friends.
order contains every integer from 1 to n exactly once, representing the IDs of the participants of a race in their finishing order.
friends contains the IDs of your friends in the race sorted in strictly increasing order. Each ID in friends is guaranteed to appear in the order array.
Return an array containing your friends' IDs in their finishing order.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn recover_order(order: Vec<i32>, friends: Vec<i32>) -> Vec<i32> {
let friends_set: std::collections::HashSet<i32> = friends.into_iter().collect();
order.into_iter().filter(|x| friends_set.contains(x)).collect()
}
}