Find the point on X | Function to find median of the array ; Sort the given array ; If number of elements are even ; Return the first median ; Otherwise ; Driver Code
function findLeastDist ( A , N ) { A . sort ( ( a , b ) => a - b ) ; console . log ( A ) ; if ( ( N % 2 ) == 0 ) { return A [ parseInt ( ( N - 1 ) / 2 ) ] ; } else { return A [ parseInt ( N / 2 ) ] ; } } var A = [ 4 , 1 , 5 , 10 , 2 ] ; var N = A . length ; document . write ( " " + findLeastDist ( A , N ) + " " + 0 + " " ) ;