Skip to content

Instantly share code, notes, and snippets.

@pjcodesjs
Created March 12, 2023 05:35
Show Gist options
  • Save pjcodesjs/850206c2a7c9f0683e8db63cd845bf7f to your computer and use it in GitHub Desktop.
Save pjcodesjs/850206c2a7c9f0683e8db63cd845bf7f to your computer and use it in GitHub Desktop.
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