“Temukan nomor tersesat” Kode Jawaban

Temukan nomor tersesat

const stray = numbers => {
  let number = 0
  
  numbers.forEach(x => numbers.indexOf(x) ? number = x : null)
  return number
}
console.log(stray([17, 17, 3, 17, 17, 17, 17]))
kouqhar

Temukan nomor tersesat

// 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)

const stray = function (numbers) {
  let number = 0

  numbers.forEach(x => { if(numbers.indexOf(x) < 1) number = x })
  numbers.forEach(x => { if(numbers.indexOf(x) > 1) number = x })

  return number
}

// With love @kouqhar
kouqhar

Jawaban yang mirip dengan “Temukan nomor tersesat”

Pertanyaan yang mirip dengan “Temukan nomor tersesat”

Lebih banyak jawaban terkait untuk “Temukan nomor tersesat” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya