What if you could instantly see every way to arrange your items without missing a single one?
Why Generate All Permutations of Array in DSA Typescript?
Imagine you have a set of unique keys and you want to try every possible order to unlock a treasure chest. Writing down all possible orders by hand or guessing randomly would take forever and be very tiring.
Trying to list all orders manually is slow and easy to mess up. You might forget some orders or repeat others. It's like trying to solve a big puzzle without a clear plan, which wastes time and causes frustration.
Using a method to generate all permutations automatically lets the computer quickly and correctly list every possible order. This saves time, avoids mistakes, and helps solve problems that need checking all arrangements.
const arr = [1, 2, 3]; // Manually write all orders const permutations = [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ];
function permute(arr: number[]): number[][] {
if (arr.length === 0) return [[]];
const result: number[][] = [];
for (let i = 0; i < arr.length; i++) {
const rest = [...arr.slice(0, i), ...arr.slice(i + 1)];
for (const perm of permute(rest)) {
result.push([arr[i], ...perm]);
}
}
return result;
}This lets you explore every possible order of items easily, which is key for solving puzzles, scheduling tasks, or testing combinations.
When planning seating for a dinner party, you might want to try every seating order to find the best arrangement where everyone is happy.
Manual listing of all orders is slow and error-prone.
Generating permutations automatically saves time and avoids mistakes.
Permutations help solve problems needing all possible arrangements.
Practice
Solution
Step 1: Understand permutations
Permutations are all possible ways to arrange elements in order.Step 2: Apply to array
Generating all permutations means listing every possible order of the array elements.Final Answer:
Listing all possible orders of the array elements -> Option AQuick Check:
Permutations = all orders [OK]
- Confusing permutations with sorting
- Thinking permutations remove duplicates
- Mixing permutations with finding max element
Solution
Step 1: Analyze input and output types
The input is an array of numbers, so type is number[]. The output is all permutations, which is an array of arrays of numbers, so number[][].Step 2: Match function signature
function permute(arr: number[]): number[][] matches input number[] and output number[][] correctly.Final Answer:
function permute(arr: number[]): number[][] -> Option CQuick Check:
Input array, output array of arrays [OK]
- Using single number instead of array as input
- Returning a single array instead of array of arrays
- Mixing string and number types incorrectly
function permute(arr: number[]): number[][] {
if (arr.length === 0) return [[]];
const result: number[][] = [];
for (let i = 0; i < arr.length; i++) {
const rest = [...arr.slice(0, i), ...arr.slice(i + 1)];
for (const perm of permute(rest)) {
result.push([arr[i], ...perm]);
}
}
return result;
}
console.log(permute([1, 2]));Solution
Step 1: Understand base case and recursion
Base case returns [[]] for empty array. For [1,2], it picks each element and permutes the rest.Step 2: Trace recursive calls for [1,2]
First i=0: arr[i]=1, rest=[2], permute([2]) returns [[2]], so push [1,2]. Then i=1: arr[i]=2, rest=[1], permute([1]) returns [[1]], so push [2,1].Final Answer:
[[1, 2], [2, 1]] -> Option AQuick Check:
Permutations of [1,2] = [[1,2],[2,1]] [OK]
- Forgetting to include all permutations
- Returning only one permutation
- Confusing order of elements
function permute(arr: number[]): number[][] {
if (arr.length === 1) return [arr];
const result: number[][] = [];
for (let i = 0; i < arr.length; i++) {
const rest = arr.slice(1);
for (const perm of permute(rest)) {
result.push([arr[i], ...perm]);
}
}
return result;
}Solution
Step 1: Analyze the slice operation
The code uses arr.slice(1) always, which removes only the first element, not the current element at index i.Step 2: Correct slice to exclude current element
To generate permutations correctly, slice should exclude arr[i], e.g., [...arr.slice(0, i), ...arr.slice(i + 1)].Final Answer:
The slice should exclude the current element, not always the first -> Option DQuick Check:
Exclude current element in slice [OK]
- Always slicing from index 1 ignoring i
- Wrong base case length check
- Returning wrong data structure
function permuteUnique(arr: number[]): number[][] {
// Your code here
}Solution
Step 1: Understand duplicate permutations problem
Duplicates in input cause repeated permutations if not handled.Step 2: Sort and skip duplicates during recursion
Sorting helps group duplicates. During recursion, skip elements equal to previous to avoid repeated branches.Step 3: Why other options fail
Using Set after generation is costly. Removing duplicates loses valid permutations. No change causes duplicates.Final Answer:
Sort the array first and skip duplicates during recursion -> Option BQuick Check:
Sort + skip duplicates = unique permutations [OK]
- Removing duplicates from input loses valid permutations
- Using Set after generation wastes time and memory
- Ignoring duplicates causes repeated permutations
Ready to prove it?
Pass the test and this pattern moves unknown
