Skip to content

Instantly share code, notes, and snippets.

@NEbere
Created May 15, 2019 10:38
Show Gist options
  • Save NEbere/d30b91c651b86bcf672b8d6c24b839c2 to your computer and use it in GitHub Desktop.
Save NEbere/d30b91c651b86bcf672b8d6c24b839c2 to your computer and use it in GitHub Desktop.
Given a string, find the non-repeating characters and return in form of an array
function findNonRepeatingChars(string) {
const stringArray = string.split('')
const nonrecurring = []
const resultObj = {}
for(let char of stringArray) {
if(!resultObj[char]) {
resultObj[char] = 1;
} else {
resultObj[char] += 1;
}
}
for(let char of stringArray) {
if(resultObj[char] == 1){
nonrecurring.push(char)
}
}
return nonrecurring
}
findNonRepeatingChars('apple')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment