This is my proposal:
function findOutlier(arr){ const even=[], odd=[]; arr.find(n => { (n%2 ? even : odd).push(n); return even.length ? odd.length > 1 : odd.length ? even.length > 1 : false; }); return even.length === 1 ? even[0] : odd.length === 1 ? odd[0] : null; }
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.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.