JSFiddle - React, Tailwind, and code Playground

by Ansmtz

JavaScript

const solve = (intArray, number) => {
  const copy = [...intArray];
  const sortedArr = intArray.sort((a, b)=>(b-a)).slice(0, number);
  const total = {};
  for(let n of sortedArr){
    total[n] = true;
  }
  const res = copy.filter((el) => total[el]);
  console.log(res);
};

solve([2,5,4], 3);