mirror of
				https://github.com/theoludwig/programming-challenges.git
				synced 2025-09-11 23:11:21 +02:00 
			
		
		
		
	feat(solution): javascript-non-repeating
This commit is contained in:
		@@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					# javascript-non-repeating - first-non-repeating-character
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Programming language : JavaScript
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Created by [@Divlo](https://github.com/Divlo) on 15 November 2020.
 | 
				
			||||||
@@ -0,0 +1,37 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @param {string} string
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function solution (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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = solution
 | 
				
			||||||
		Reference in New Issue
	
	Block a user