# Daily Challenge #35 - Find the Outlier

### dev.to staff γ»1 min read

In this challenge, you'll be given an array with a length of at least three, containing (possibly quite large) integers. The array is either comprised of entirely odd integers or even integers with one exception. Write a method that returns the integer that is not like the others.

**Example:**

`findOutlier([2, 4, 0, 100, 4, 11, 2602, 36])`

=> `11`

**Additional practice arrays:**

[160, 3, 1719, 19, 11, 13, -21]

[4, 8, 15, 16, 24, 42]

[16, 6, 40, 66, 68, 28]

Good luck, happy coding~!

*This challenge comes from user obnounce. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!*

*Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!*

JavaScript π

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 πYou only need to fetch the firsts 3 items to know what you are searching for.

This goes through the list of numbers only a single time, stopping early if possible. Could probably be code golfed further π

I have done a Ruby one, based on counting the number of even values in the first three elements of the array to switch the logic between finding the first even and the first odd element.

Atleast it works somehow π

Elixir:

A javascript one that has pretty reasonable complexity

in JavaScript. o(n), uses constant space.

in the worst case (outlier is at the end of the loop) it will go through all items. but if the outlier is found before, the loop stops.

Bad solution! See comments below!Ruby solution, I tried to do something different.

When the list has no exception, the first element is returned; except when

`numbers`

looks like`[3,3]`

(bug that you canfixby adding`.uniq`

when computing the sum.)I believe this would fail if the outlier is even and the rest are an

oddnumber of odd numbers. The examples all have anevennumber of odd numbers. Can you check that?It seems that I canβt make it right and as simple as it was. So Iβll stick with this solution:

Dart solution:

Link to the online playground: dartpad.dartlang.org/d5e83e228c72d...

This is the shortest solution I can create.

ruby 2.7

This is my proposal:

In this case, Array.find will stop looping when

boththeevenandoddarrays have at least one item.There is

only one loopon the array values, and this loop stops just when the "strange element" is found.At the end, the group with just one element has the solution.

In the last test, an array formed entirely by even numbers,

nullis returned.With JS

Explanation:

`even`

and`odd`

numbers.Here's my code:

I believe I could have used filters on the arrays method but just decided to follow the long step.

Here's my code:

I believe I could have used filters on the arrays method but just decided to follow the long step.

Here's my code:

I believe I could have used filters on the arrays method but just decided to follow the long step.

Not elegant, but it'll do the job.

array = [500,502,1002,1234,601]

odd = even = i = 0

oddValue = evenValue = 0

a = len(array)

while (i < a):

if (array[i] % 2):

oddValue = array[i]

odd += 1

else:

evenValue = array[i]

even += 1

i += 1

if (odd > even == 1):

print(evenValue)

elif (odd == 1):

print(oddValue)

else:

print("Wrong list of numbers")

## Ruby Language

## With specs!

## output

array = [500,502,1002,1234,601]

odd = even = i = 0

oddValue = evenValue = 0

a = len(array)

while (i < a):

if (array[i] % 2):

oddValue = array[i]

odd += 1

else:

evenValue = array[i]

even += 1

i += 1

if (odd > even == 1):

print(evenValue)

elif (odd == 1):

print(oddValue)

else:

print("Wrong list of numbers")

Python :