DEV Community

Discussion on: Daily Challenge #35 - Find the Outlier

Collapse
 
frncesc profile image
Francesc Busquets

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.