1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| const arr = [1, 1, 1, 1, 1, 1, 2]; const demo = [3, 2, 2, 2, 2, 2, 2, 2];
function findDiff(arr) { for (let i = 1; i < arr.length; i++) { if (arr[i - 1] === arr[i]) { continue; } if (arr[i] === arr[i + 1]) { return i - 1; } if (arr[i - 1] === arr[i + 1]) { return i; } return i; } }
console.log(findDiff(arr));
|