Skip to content

Instantly share code, notes, and snippets.

@megantaylor
Created June 14, 2018 19:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save megantaylor/88da642f9bd2e382623f8135eed67a08 to your computer and use it in GitHub Desktop.
Save megantaylor/88da642f9bd2e382623f8135eed67a08 to your computer and use it in GitHub Desktop.
find the first recurring character in a string
// Given a string, return the first recurring character in it, or null if there is no recurring chracter. For example, given the string "acbbac", return "b". Given the string "abcdef", return null.
const arr = "acbbac".split("");
const arr2 = "abcdef".split("");
function findFirstDuplicate(arr){
let item = null;
for (var i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) !== i) {
item = arr[i];
break;
}
}
return item;
}
console.log("first dupe", findFirstDuplicate(arr));
console.log("no dupe", findFirstDuplicate(arr2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment