JavaScript 😀

Not efficient, as it's iterating the array twice (I could've used reduce to iterate once but well... this looks more readable)

reduce

BTW, the last one looks weird, as it contains only even numbers.

const isEven = n => n % 2 === 0 const isOdd = n => n % 2 === 1 const findOutlier = arr => { const odds = arr.filter(isOdd) const evens = arr.filter(isEven) return odds.length < evens.length ? odds[0] : evens[0] } [ [2, 4, 0, 100, 4, 11, 2602, 36], [160, 3, 1719, 19, 11, 13, -21], [4, 8, 15, 16, 24, 42], [16, 6, 40, 66, 68, 28] ].map(findOutlier)

There's a potential bug.

-21 will fail both isOdd and isEven function.

-21

isOdd

isEven

The function isOdd should be

const isOdd = n => n % 2 !== 0

Since negative odd numbers mod 2 equals to -1 instead of 1.

-1

1

Whoa.... you've just caught years worth of bugs I have possibly created... 😅

Thank you @Hao for finding the issue. 😀 I really didn't think that thru. 👍

Here is the updated code

let isEven = n => n % 2 === 0 let isOdd = n => n % 2 !== 0 const findOutlier = arr => { const odds = arr.filter(isOdd) const evens = arr.filter(isEven) return odds.length < evens.length ? odds[0] : evens[0] } [ [2, 4, 0, 100, 4, 11, 2602, 36], [160, 3, 1719, 19, 11, 13, -21], [4, 8, 15, 16, 24, 42], [16, 6, 40, 66, 68, 28], [-21, 1, 2, 3] ].map(findOutlier)

I think I would probably use !isEven next time 😎

!isEven

We're a place where coders share, stay up-to-date and grow their careers.

We strive for transparency and don't collect excess data.

## re: Daily Challenge #35 - Find the Outlier VIEW POST

FULL DISCUSSIONJavaScript 😀

Not efficient, as it's iterating the array twice (I could've used

`reduce`

to iterate once but well... this looks more readable)BTW, the last one looks weird, as it contains only even numbers.

There's a potential bug.

`-21`

will fail both`isOdd`

and`isEven`

function.The function

`isOdd`

should beSince negative odd numbers mod 2 equals to

`-1`

instead of`1`

.Whoa.... you've just caught years worth of bugs I have possibly created... 😅

Thank you @Hao for finding the issue. 😀

I really didn't think that thru. 👍

Here is the updated code

I think I would probably use

`!isEven`

next time 😎