DEV Community

Cover image for Codewars - Find The Parity Outlier
Nicolás Bost
Nicolás Bost

Posted on

Codewars - Find The Parity Outlier

Salutations.

Dave says hi

I'm posting Codewars challenges and my thought process in this series. I'm using JS and Node 18 whenever possible. Just for the sake of clarity, I'm making fair use of them.

The problem says we need to find either of these: the odd number in an array full of even numbers, or the even number in an array full of odd numbers. That is to say:
[ 0, 2, 4, 6, 8, 97, 14, 14, -2 ] -> 97
[ 1, 3, 2, -3, -7, 227, -183 ] -> 2


First, I'll check what kind of outlier is by looking at the first 3 elements of the array:

  • 3 even elements =>
    0i2(ximod2)=0\sum_{\substack{0\leq i\leq 2}} ( x_{i}\mod 2 ) = 0
  • 1 odd element =>
    0i2(ximod2)=1\sum_{\substack{0\leq i\leq 2}} ( x_{i}\mod 2 ) = 1
  • 2 odd elements =>
    0i2(ximod2)=2\sum_{\substack{0\leq i\leq 2}} ( x_{i}\mod 2 ) = 2
  • 3 odd elements =>
    0i2(ximod2)=3\sum_{\substack{0\leq i\leq 2}} ( x_{i}\mod 2 ) = 3

All we need to do afterwards is traverse the array until we hit the one number whose modulo-2 is different. Two variations of said algorithm are applied accordingly:

// odd number in even array
while (number % 2 == 0 && counter < integers.length){
      number = integers[counter];
      counter++;
    }
// even number in odd array
while (number % 2 == 1 && counter < integers.length){
      number = integers[counter];
      counter++;
    }
Enter fullscreen mode Exit fullscreen mode

Assembling the parts together we end up with:


function findOutlier(integers){
  let counter = 0;
  let number = 0;

  let a = integers[0] % 2;
  let b = integers[1] % 2;
  let c = integers[2] % 2;


  if (a + b + c <= 1){
    number = integers[0];
    while (number % 2 == 0 && counter < integers.length){
      number = integers[counter];
      counter++;
    }
  }
  else {
    number = integers[0];
    while (number % 2 == 1 && counter < integers.length){
      number = integers[counter];
      counter++;
    }
  }
  return number;
}
Enter fullscreen mode Exit fullscreen mode

And this is exactly how I felt when it didn't pass the tests:

Oh no


Second, I had to google what happened. Turns out I didn't consider negative numbers could appear. And so I had to read how JS deals with negative modulos


For the third step, use Math.abs():

function findOutlier(integers){
  let counter = 0;
  let number = 0;

  let a = Math.abs(integers[0] % 2);
  let b = Math.abs(integers[1] % 2);
  let c = Math.abs(integers[2] % 2);


  if (a + b + c <= 1){
    number = integers[0];
    while (Math.abs(number) % 2 == 0 && counter < integers.length){
      number = integers[counter];
      counter++;
    }
  }
  else {
    number = integers[0];
    while (Math.abs(number) % 2 == 1 && counter < integers.length){
      number = integers[counter];
      counter++;
    }
  }
  return number;
}
Enter fullscreen mode Exit fullscreen mode

Decent performance, not so readable though. It has ample room for improvement.

Take care. Drink water 💧💧💧.

Previous

Top comments (0)