Home >Web Front-end >JS Tutorial >Codewars - Find The Parity Outlier

Codewars - Find The Parity Outlier

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-05 19:30:47727browse

Salutations.

Codewars - Find The Parity Outlier

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)=0sum_{substack{0leq ileq 2}} ( x_{i}mod 2 ) = 0 0≤i≤2(ximod2)=0
  • 1 odd element =>
    0i2(ximod2)=1sum_{substack{0leq ileq 2}} ( x_{i}mod 2 ) = 1 0≤i≤2(ximod2)=1
  • 2 odd elements =>
    0i2(ximod2)=2sum_{substack{0leq ileq 2}} ( x_{i}mod 2 ) = 2 0≤i≤2(ximod2)=2
  • 3 odd elements =>
    0i2(ximod2)=3sum_{substack{0leq ileq 2}} ( x_{i}mod 2 ) = 3 0≤i≤2(ximod2)=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++;
    }

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;
}

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

Codewars - Find The Parity Outlier


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;
}

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

Take care. Drink water ???.

Previous

The above is the detailed content of Codewars - Find The Parity Outlier. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn