Skip to content

Instantly share code, notes, and snippets.

@lancekruegger
Created September 25, 2015 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancekruegger/f9e77fc3562df1e662c9 to your computer and use it in GitHub Desktop.
Save lancekruegger/f9e77fc3562df1e662c9 to your computer and use it in GitHub Desktop.
function getDuplicates(str)
{
var duplicates = "";
while (str.length > 0) {
// Adds a placeholder for the string at the current position before we run replace
var cur_str = str;
// Searches for all instances of char in string and replaces them with nothing
var regex = new RegExp(str[0], "g");
str = str.replace(regex, "");
// If the string differs by more than one we know that duplicates were found within the string
if (cur_str.length - str.length > 1) {
duplicates += cur_str[0];
}
}
// Returns a string of duplicates
return duplicates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment