Created
March 12, 2023 05:35
-
-
Save pjcodesjs/850206c2a7c9f0683e8db63cd845bf7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findFirstRepeatingElement(arr) { | |
let count = {}; | |
for (let i = 0; i < arr.length; i++) { | |
if (count[arr[i]]) { | |
return arr[i]; | |
} else { | |
count[arr[i]] = 1; | |
} | |
} | |
return null; | |
} | |
// Example usage: | |
const arr = [2, 5, 1, 2, 3, 5, 1, 2, 4]; | |
const firstRepeating = findFirstRepeatingElement(arr); | |
console.log(firstRepeating); // Output: 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment