Last active
February 21, 2020 00:27
-
-
Save elitenomad/0075af946fcc7ee9fd383f776b4d4d9f to your computer and use it in GitHub Desktop.
Remove duplicate strings Javascript
This file contains hidden or 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 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