본문 바로가기
알고리즘/Codewars

[Codewars] Find the stray number (7 kyu) / JavaScript

by fluss 2023. 4. 20.

https://www.codewars.com/kata/57f609022f4d534f05000024

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

You are given an odd-length array of integers, in which all of them are the same, except for one single number.

Complete the method which accepts such an array, and returns that single different number.

The input array will always be valid! (odd-length >= 3)

 

Examples

[1, 1, 2] ==> 2
[17, 17, 3, 17, 17, 17, 17] ==> 3
 

코드

function stray(numbers) {
  const set = [...new Set(numbers)];
  return numbers.filter(el => el === set[0]).length === 1 ? set[0] : set[1];
}

댓글