Created
May 22, 2020 17:32
-
-
Save dotsara/b5531b420e27eb675ab403a92369b239 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Finding the first non-repeating character</title> | |
</head> | |
<body> | |
<style> | |
</style> | |
<script> | |
// sample strings you could use: | |
const sampleA = "aabccddeffgh"; | |
const sampleB = "abbccccdddeff"; | |
console.log("Ready-made strings to use if you want!"); | |
console.log(`"sampleA": ${sampleA}`); | |
console.log(`"sampleB": ${sampleB}`); | |
console.log("Otherwise, run firstNonRepeatingCharacter with your own string!"); | |
function firstNonRepeatingCharacter(string) { | |
let characters = mappedCharacters(string); | |
for (let i in characters) { | |
if (characters[i] === 1) { | |
return `The first non-repeating character is: ${i}`; | |
} | |
} | |
return "All the characters repeat, you sneak."; | |
} | |
function mappedCharacters(string) { | |
const map = {}; | |
for (let i = 0; i < string.length; i++) { | |
!map[string[i]] ? map[string[i]] = 1 : | |
map[string[i]]++; | |
} | |
return map; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment