How to find the second biggest number in a set of random numbers in TypeScript

1 Answer

0 votes
function findSecondMax(total: number, rndmax: number): number | null {
  const numbers: number[] = Array.from({ length: total }, () => {
    const n = Math.floor(Math.random() * rndmax) + 1;
    console.log(n);

    return n;
  });

  const uniqueNumbers: number[] = [...new Set(numbers)].sort((a, b) => b - a);
  
  return uniqueNumbers.length > 1 ? uniqueNumbers[1] : null;
}

const secondMax: number | null = findSecondMax(10, 100);

if (secondMax !== null) {
  console.log(`The second biggest number is: ${secondMax}`);
} else {
  console.log("Not enough unique numbers to determine a second maximum.");
}




/*
run:

64 
81 
37 
67 
13 
46 
25 
66 
11 
78 
"The second biggest number is: 78" 

*/

 



answered Oct 4, 2025 by avibootz
...