HashTable - rs-hash/GETTHATJOB GitHub Wiki
1. ReturnFirstRecurringCharacter
function firstRecurringCharacter(arr) {
const seen = new Set();
for (let num of arr) {
if (seen.has(num)) {
return num; // Found the first recurring character
}
seen.add(num);
}
return undefined; // No recurring character found
}
console.log(firstRecurringCharacter([2,5,1,2,3,5,1,2,4])); // Output: 2
console.log(firstRecurringCharacter([2,1,1,2,3,5,1,2,4])); // Output: 1
console.log(firstRecurringCharacter([2,3,4,5])); // Output: undefined
Time and Space Complexity:
Time Complexity: O(n) — only one pass through the array.
Space Complexity: O(n) — in the worst case, we store all unique elements in the Set