function fn(a) { a = a.slice().sort(function(a, b) { return a - b }); return a[a.indexOf(Math.max.apply(null, a))-1] }; alert( fn([-1, -2, -5, -25, -125]) ); //-2 alert( fn([1, 1, 5, 5]) ); //1 alert( fn([1, 2, 3, 4, 5, 5]) ); //4