DEV Community

Discussion on: Filtering out a stray number in an array

Collapse
 
bjhaid_93 profile image
/[Abejide Femi Jr]\s/ • Edited
//my solution in javascript
function stray(numbers) {
  let a = numbers.sort((a,b)=>a-b)
    if (a[0] !== a[1]) return a[0]
    else {
        return a[a.length -1]
    }
}
stray([1, 1, 2])

Collapse
 
dance2die profile image
Sung M. Kim • Edited

That's a great work around.

Would you be able to do it in O(n) time?

numbers.sort usually be O(n log n) (unless using TimSort and data is already sorted).