DEV Community

loading...

Discussion on: Coding Puzzles: Week of 4/8

Collapse
ahmedmusallam profile image
Ahmed Musallam • Edited

here my cheap solution:

function stray(array) {
    var sorted = array.sort((a,b) => a - b)
    if (sorted[0] === sorted[1]) return sorted.pop();
    else return sorted.shift()
}

or shorter and more unreadable:

function stray(array) {
    var sorted = array.sort((a,b) => a - b)
    return sorted[0] === sorted[1] ? sorted.pop() : sorted.shift();
}