If I have an array of numbers and I want get the number nearest to average, how can I get that number?
I've tried to sort the array in an ascending order, followed by retrieving the middle element which is the average.
Note: I used Math.floor() in case if the length of the array is odd not even number
const arr = [1, 6, 10, 3, 15, 9, 4, 7];
const arrSorted = arr.sort((a,b)=>a-b);
const nearToAVG = arrSorted[Math.floor(arr.length/2)];
console.log(nearToAVG); // 7
Are there any better ways of getting the number near to the average in array?