How to count the total of all pairs permutations in an array with TypeScript

1 Answer

0 votes
const arr1: number[] = [1, 8, 5];
 
// The pairs are: (1, 8), (1, 5), (8, 1), (8, 5), (5, 1), (5, 8)
 
let total_pairs: number = arr1.length * (arr1.length - 1);
console.log("Total Pairs = " + total_pairs);
 
const arr2: number[] = [1, 8, 5, 2];
 
// The pairs are: (1, 8), (1, 5), (1, 2), (8, 1), (8, 5), (8, 2),
//                (5, 1), (5, 8), (5, 2), (2, 1), (2, 8), (2, 5)
 
total_pairs = arr2.length * (arr2.length - 1);
console.log("Total Pairs = " + total_pairs);
 
 
 
/*
run:
 
"Total Pairs = 6" 
"Total Pairs = 12" 
 
*/

 



answered Jun 17, 2024 by avibootz
...