Created
August 7, 2018 20:02
-
-
Save sandrabosk/fd6621a0958d0832836b971268c4e1ca to your computer and use it in GitHub Desktop.
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
// this solution is the most efficient because it walks the array only once and | |
// it doesn't use any helper methods | |
const firstNonRepeatingLetterA = (str) => { | |
let theObj = {}; | |
for(let i = 0; i< str.length; i++){ | |
if(theObj[str[i]]){ | |
theObj[str[i]]+=1; | |
} else { | |
theObj[str[i]] = 1; | |
} | |
} | |
return theObj; | |
} | |
// I looked up string methods and I learned that .lastIndexOf() is super cool because | |
// returns the index inside the string of the very last occurrence of the character, | |
// and it searches backwards from the index. If it doesn't find the character, returns -1 | |
const firstNonRepeatingLetterB = (str) => { | |
if(str.length === 1){ | |
return str; | |
} | |
if(!str){ | |
return "You passed the empty string" ; | |
} | |
str = str.toLowerCase(); | |
for(let i = 0; i < str.length; i++){ | |
if(str.indexOf(str[i]) === str.lastIndexOf(str[i])){ | |
return str[i]; | |
} | |
} | |
return "The passed string doesn\'t have non repetitive letters" ; | |
} | |
// I can't check the tests, I still might fail some of them. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment