DEV Community

loading...

Discussion on: Coding Puzzles: Week of 4/8

Collapse
clandau profile image
Courtney • Edited

This is the first I'm finding this, excited to play along! Here is yesterday's (edited to make the colors show up):

function stray(numbers) {
  let num1 = numbers[0], num2;
  let dupOf1 = false;
  for(let i=1; i<numbers.length; i++) {
      if(numbers[i] === num1) {
        if(num2) return num2;
        else dupOf1 = true;
      }
      else if(numbers[i] === num2) return num1;
      else num2 = numbers[i];
    }
  return dupOf1 ? num2 : num1;
}