find first non-repeating character
in a string
[ for-of loop | Map | set | get ]

Find first non-repeating character

Write a function that takes a string as input and returns the first non-repeated character in that string.

If all characters are repeated, return null.


Example ...

Enter a string ...

"AAABBBCDDDEEEF" string

The function returns "C" → the first non-repeated character in the string, the character only appears once.

The function returns null if each character appears more than once.

Strings are a sequence of zero or more characters written inside quotes used to represent text.

Strings may consist of letters, numbers, symbols, words, or sentences.

Strings are immutable, they cannot be changed.

Each character in a string has an index.

The first character will be index 0 the second character will be index 1 and so on.

There are two ways to access an individual character in a string.

charAt() method

const str1 = "abc"; string

str1.charAt(0); character at index 0 → "a"

str1.charAt(1); character at index 1 → "b"

str1.charAt(2); character at index 2 → "c"

str1.charAt(3); character at index 3 → "" index not found

Alternatively use at() or slice() methods

bracket notation []

const str2 = "abc"; string

str2[0]; character at index 0 → "a"

str2[1]; character at index 1 → "b"

str2[2]; character at index 2 → "c"

str2[3]; character at index 3 → undefined index not found


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Find first non-repeating character in a string using ↴

for-of loop → iterates through the values of an iterable object, such as an Array, String, Set, Map, ...

Map → collection of key/value pairs.

set() method → allows elements to be added to a Map.

get() method → gets the value associated with the specified key in a Map.


for-of loop iterates through the values of an iterable object, such as an Array, String, Set, Map, ...

syntax

for (variable of iterable) {}

variable holds the current value of the iteration.

of keyword indicates that the loop should iterate over the values of the iterable.

iterable object that is iterable, such as an Array, String, Set, Map, ...

Iterate over each character in the string.

const str3 = "ABC";

for (const char of str3) {

console.log(char);

} returns ↴

A

B

C → printed to console

The loop will run three times, once for each character in the string.

On each iteration, the value of the current element is stored in the variable char

For each iteration of the loop, the current value of char is printed to the console.


Map is a collection of key/value pairs that can use any data type as a key and can maintain the order of its entries.

Maps have elements of both Arrays (an ordered collection) and Objects (a unique key/value pair collection).

The size and order of entries is preserved like an Array, the entries themselves are key/value pairs like an Object and can be of any data type.

Maps can be initialized with the new Map() syntax.

const map = new Map(); empty map

set() method allows elements to be added to a Map.

The first argument will be the key, and the second argument will be the value.

const map = new Map();

map.set("A", 1);

map.set("B", 2);

map.set("C", 3); returns ↴

Map(3) {"A" => 1, "B" => 2, "C" => 3}

Map uses the => syntax to signify key/value pairs as key => value

get() method gets the value associated with the specified key

map.get("A"); 1

map.get("B"); 2

map.get("C"); 3

map.get("D"); undefined


Initialize a variable to hold the string to find first non-repeating character.

const string1 = "AABBCDDE"; → user input


Define a function firstNonRepeatedChar to find first non-repeating character.

function firstNonRepeatedChar(str) {}

The function takes a string as input str and returns the first non-repeated character in that string.

The function counts the number of occurrences of each character and stores them in a Map.

By iterating through the string twice, it first builds a count of each character and then identifies the first character that appears only once.

If all characters repeat more than once anywhere in the string, it returns null

By utilizing a Map for character counting, the function operates efficiently with a time complexity of O(n) where n is the length of the string. This approach avoids the need for nested loops

Create a Map to store character counts.

const charCount = new Map() charCount

The function employs two loops: the first for counting characters and the second for finding the first non-repeated character.

Loop 1:

A variable char is used to represent the current item of the iterable being looped through.

for (const char of str) {

charCount.set(char, (charCount.get(char) || 0) + 1)

}

for-of loop iterates over each character in str, updating the charCount Map.

For each character, if it is encountered for the first time, initialize its count to 0

otherwise increment its existing count by 1

const str = "AABBCDDE";

const charCount = new Map();

for (const char of str) {

charCount.set(char, (charCount.get(char) || 0) + 1);

console.log(charCount);

} returns ↴

Map(1) {"A" => 1} "A"

Map(1) {"A" => 2} "A"

Map(2) {"A" => 2, "B" => 1} "B"

Map(2) {"A" => 2, "B" => 2} "B"

Map(3) {"A" => 2, "B" => 2, "C" => 1} "C"

Map(4) {"A" => 2, "B" => 2, "C" => 1, "D" => 1} "D"

Map(4) {"A" => 2, "B" => 2, "C" => 1, "D" => 2} "D"

Map(5) {"A" => 2, "B" => 2, "C" => 1, "D" => 2, "E" => 1} "E"

Count of occurrences of each character ↴

"A" 2 occurrences

"B" 2 occurrences

"C" 1 occurrence

"D" 2 occurrences

"E" 1 occurrence

Loop 2:

The second loop is another for-of loop and it checks each character in str against the charCount Map.

Does the current char in charCount have a count of exactly 1

charCount[char] === 1

for (const char of str) {

if (charCount.get(char) === 1) { 1 only

return char first char with a count of 1

}

}

The first character with a count of 1 is returned as the result.

"C" is the first character with a count of 1

If no non-repeated character is found, return null.

return null


Call the function with ↴

firstNonRepeatedChar(string1);


Find first non-repeating character in string.

const string1 = "AABBCDDE";

function firstNonRepeatedChar(str) {

const charCount = new Map();

for (const char of str) {

charCount.set(char, (charCount.get(char) || 0) + 1);

}

for (const char of str) {

if (charCount.get(char) === 1) {

return char;

}

}

return null;

}

call function

firstNonRepeatedChar(string1); returns ↴

"C"

Find first non-repeating character