Last active
December 3, 2019 10:07
-
-
Save infinitypaul/d0d13fc1dc3d4660598f6b8f7a6cae53 to your computer and use it in GitHub Desktop.
Remove Duplicate Character
This file contains 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 characterOnce(str){ | |
let frequency = {}; | |
let newWord =''; | |
for(let i=0; i<str.length; i++){ | |
let character = str.charAt(i) | |
if(frequency[character]){ | |
frequency[character]++ | |
} else { | |
frequency[character] = 1 | |
} | |
} | |
for (let property in frequency) { | |
if(frequency[property] < 2){ | |
newWord += property | |
} | |
} | |
return newWord; | |
} | |
characterOnce('appple'); | |
//This was the way i was solving it the first time;;; | |
function secondApproach(str){ | |
let neWord = ''; | |
let reArrange = ''; | |
for(let t of str){ | |
if(reArrange.includes(t)){ | |
neWord = neWord.replace(t,''); | |
} else { | |
neWord += t | |
reArrange += t | |
} | |
} | |
return neWord; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment