1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2025-05-18 12:02:53 +02:00

feat: rewrite programming-challenges CLI (#3)

This commit is contained in:
Divlo
2021-06-09 20:31:45 +02:00
committed by GitHub
parent 7aa12f313e
commit 677a55a9d8
256 changed files with 16829 additions and 1881 deletions

View File

@ -0,0 +1,3 @@
# first-non-repeating-character/javascript/function
Created by [@Divlo](https://github.com/Divlo) on 6 June 2021.

View File

@ -0,0 +1,3 @@
{
"type": "module"
}

View File

@ -0,0 +1,46 @@
import readline from 'readline'
const input = []
const readlineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
})
readlineInterface.on('line', (value) => {
input.push(value)
})
readlineInterface.on('close', solution)
const firstNonRepeatingCharacter = (string) => {
const lettersCount = {}
for (let index = 0; index < string.length; index++) {
const character = string[index]
if (lettersCount[character] == null) {
lettersCount[character] = {
total: 1,
firstIndex: index,
value: character
}
} else {
lettersCount[character].total += 1
}
}
let result = null
for (const character in lettersCount) {
const characterObject = lettersCount[character]
if (characterObject.total === 1) {
if (result == null) {
result = characterObject
} else if (characterObject.firstIndex < result.firstIndex) {
result = characterObject
}
}
}
if (result == null) {
return ''
}
return result.value
}
function solution() {
console.log(firstNonRepeatingCharacter(input[0]))
}