JSFiddle - React, Tailwind, and code Playground
by JinHan Heo
JavaScript
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const length = A.length - 1
let firstIndex = 0
let secondIndex = 2
let first = A[0]
let second = A[2]
let result = 0
if (first !== second) {
return first
}
if (A.length === 0) {
throw new Error('less than zero')
}
for (let i=0; i<length; i++) {
firstIndex = secondIndex % 2 === 0 ? firstIndex + 1 : firstIndex + 3
secondIndex = secondIndex % 2 === 0 ? secondIndex + 1 : secondIndex + 3
first = A[firstIndex]
second = A[secondIndex]
if (first !== second) {
result = first
return
}
}
return result
}
solution([9, 3, 9, 3, 9, 7, 9])