Find the value whose second occurrence comes first
A conference check-in kiosk logs every badge it scans. A badge scanned twice usually means a shared or cloned badge, and security wants to know about the first one as soon as it happens. Implement findFirstRepeat(values), which walks the log from left to right and reports the first moment a value shows up for the second time.
findFirstRepeat(values) // => { value, firstIndex, repeatIndex } | null
[4, 9, 2, 9, 4], 9 repeats at index 3, before 4 repeats at index 4.firstIndex is the index of that value’s first occurrence, repeatIndex the index of its second, and value is values[firstIndex].null when every value is distinct, including for an empty array.Set and Array.prototype.includes use: NaN equals NaN, 0 equals -0, values of different types never match (1 and '1' differ), and objects match only if they are the same reference.values. Arrays can hold up to 50,000 values, so comparing every pair is too slow.findFirstRepeat(['B12', 'C07', 'A33', 'C07', 'B12']);
// => { value: 'C07', firstIndex: 1, repeatIndex: 3 }
findFirstRepeat([4, 9, 2, 9, 4]); // => { value: 9, firstIndex: 1, repeatIndex: 3 }
findFirstRepeat([1, '1', true, 1]); // => { value: 1, firstIndex: 0, repeatIndex: 3 }
findFirstRepeat([NaN, 5, NaN]); // => { value: NaN, firstIndex: 0, repeatIndex: 2 }
findFirstRepeat([0, -0]); // => { value: 0, firstIndex: 0, repeatIndex: 1 }
findFirstRepeat([{}, {}]); // => null (two different objects)
findFirstRepeat(['a', 'b', 'c']); // => null
findFirstRepeat([]); // => null