JSFiddle - React, Tailwind, and code Playground
by AlexGun
JavaScript
function figureOut(arr) {
const state = {}
for (let i = 0; i < arr.length; i++) {
let currentElem = arr[i]
for (let j = 0; j < currentElem.length;j++) {
if (currentElem[j] === " ") continue
state[currentElem[j]] ? state[currentElem[j]].push(j) : state[currentElem[j]] = [j]
let currentLetter = state[currentElem[j]]
let length = currentLetter.length
if (length > 1) {
console.log(state, currentElem[j],currentLetter)
if (Math.abs(currentLetter[length-1] - currentLetter[length-2]) > 1){
return currentElem[j]
}
}
}
}
return null
}
console.log(figureOut(["a b", " ba"]));
console.log(figureOut(["icwth", "wicth", "witch"]));