Created
May 15, 2019 10:38
-
-
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
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 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