Skip to content

Latest commit

 

History

History
24 lines (19 loc) · 888 Bytes

arrayElementParity.md

File metadata and controls

24 lines (19 loc) · 888 Bytes
  • In this Kata, you will be given an array of integers whose elements have both a negative and a positive value, except for one integer that is either only negative or only positive. Your task will be to find that integer.

Examples:

[1, -1, 2, -2, 3] => 3 // 3 has no matching negative appearance

[-3, 1, 2, 3, -1, -4, -2] => -4 // -4 has no matching positive appearance

[1, -1, 2, -2, 3, 3] => 3 // (the only-positive or only-negative integer may appear more than once)

Good luck!

Solution:

function solve(arr){
    return arr.find(num => !arr.includes(-num));
};

console.log(solve([1,-1,2,-2,3])); // 3
console.log(solve([-3,1,2,3,-1,-4,-2])); // -4
console.log(solve([1,-1,2,-2,3,3])); // 3
console.log(solve([-110,110,-38,-38,-62,62,-38,-38,-38])); // -38