Skip to content

Instantly share code, notes, and snippets.

@elitenomad
Last active February 21, 2020 00:27
Show Gist options
  • Save elitenomad/0075af946fcc7ee9fd383f776b4d4d9f to your computer and use it in GitHub Desktop.
Save elitenomad/0075af946fcc7ee9fd383f776b4d4d9f to your computer and use it in GitHub Desktop.
Remove duplicate strings Javascript
function removeDuplicate(str) {
arr = []
prev_value = ''
splitted_str = str.split(' ')
splitted_str.forEach((s) => {
console.log("prev value", prev_value)
console.log("s value", s)
if(s !== prev_value) {
arr.push(s);
prev_value = s
}else{
return;
}
})
console.log(arr.join(' '))
}
// Use case 1:
"This is a test string string"
// Use Case 2:
"This This is a test string"
// Use case 3:
"This is is a a test test string"
// Use case 3:
"This is is a test string and it is a good string"
// We can make solution look fancier :) by using Set data structure from JS to remove the duplicates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment