Skip to content

Instantly share code, notes, and snippets.

@infinitypaul
Last active December 3, 2019 10:07
Show Gist options
  • Save infinitypaul/d0d13fc1dc3d4660598f6b8f7a6cae53 to your computer and use it in GitHub Desktop.
Save infinitypaul/d0d13fc1dc3d4660598f6b8f7a6cae53 to your computer and use it in GitHub Desktop.
Remove Duplicate Character
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