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 compri...
For further actions, you may consider blocking this person and/or reporting abuse
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 bothisOdd
andisEven
function.The function
isOdd
should beSince negative odd numbers mod 2 equals to
-1
instead of1
.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.
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.
This goes through the list of numbers only a single time, stopping early if possible. Could probably be code golfed further 😛
Atleast it works somehow 😅
A javascript one that has pretty reasonable complexity
Elixir:
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 can fix by adding.uniq
when computing the sum.)I believe this would fail if the outlier is even and the rest are an odd number of odd numbers. The examples all have an even number 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:
Python
Erlang.
Go:
Here is the simple solution with PHP:
With JS
Explanation:
even
andodd
numbers.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")
Not elegant, but it'll do the job.
This is the shortest solution I can create.
ruby 2.7
Dart solution:
Link to the online playground: dartpad.dartlang.org/d5e83e228c72d...
This is my proposal:
In this case, Array.find will stop looping when both the even and odd arrays have at least one item.
There is only one loop on 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, null is returned.
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.
Python :
this only iterates the array 3 times, and with that you can know if the number is even or odd and with the find it will only iterate until it finds it, when it finds it it will stop and so I will not have to iterate over the array in case it has 10000000000 elements and it is in a close position.
what do you guys think?
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