find first non-repeating character
in a string
[ for loop | slice | includes | charAt ]

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 loop → executes a block of code a number of times.

slice() method → extracts a part of a string and returns it as a new string, without modifying the original string.

includes() method → returns true if a string contains a specified value, otherwise returns false.

charAt() method → returns the character at a specified index in a string.


for loop repeatedly executes a block of code until a specified condition evaluates to false.

The loop runs a block of code a set number of times, defined by an initialization, a condition, and an increment.

for (let x = 0; x < 4; x++) {

console.log(x);

}

Loop variable x is initialized to 0

Condition x < 4 is checked before each iteration.

The loop will continue to run as long as x is less than 4

The loop repeatedly executes a block of code 4 times, from 0 to 3

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

After each iteration, x is incremented by 1 x++

When x reaches 4 the condition evaluates to false, terminating the loop.

0

1

2

3 → printed to console


slice() method extracts a part of a string and returns it as a new string, without modifying the original string.

syntax ↴

slice(start) return a new string from start index to end of string

slice(start, end) return a string from start index to end index of string (exclusive).

Return a new string from index 1 to end of string.

const str3 = "Hello World";

str3.slice(1); start index is 1

returns ↴

"ello World"

Return a new string from index 1 to index 9 (exclusive).

const str4 = "Hello World";

str4.slice(1, 9); start index is 1 and end index is 9 (not included)

returns ↴

"ello Wor"


includes() method returns true if a string contains a specified value, otherwise, it returns false

const str5= "Hello World";

str5.includes("e"); returns boolean ↴

true"e" found in string

const str6 = "Hello World";

str6.includes("a"); returns boolean ↴

false"a" NOT found in string

logical NOT ! syntax converts a true value to a false and vice-versa.

!str6.includes("a"); returns boolean ↴

true"a" NOT found in string


charAt() method returns the character at the given index.

If the index is out of range an empty string "" is returned.

const str7 = "hello";

str7.charAt(0); "h"

str7.charAt(1); "e"

str7.charAt(2); "l"

str7.charAt(3); "l"

str7.charAt(4); "o"

str4.charAt(5); "" not found


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

const string1 = "AABBCDDEEFF"; → 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.

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

Loop through each character in str

for (let x = 0; x < str.length; x++) {}

Get the substring before the current character x

let before = str.slice(0, x) before

Get the substring after the current character x

let after = str.slice(x + 1) after

Check if the current character x is not in either before or after substrings.

if (!before.includes(str[x]) && !after.includes(str[x])) {}

It will return true if current character x NOT found in either substring.

Return the first non-repeated character.

return str.charAt(x)

Return null if no non-repeated character is found.

return null


Call the function with ↴

firstNonRepeatedChar(string1);


Find first non-repeating character in string.

const str = "ABCDE";

Initialize a variable to hold the substring before the current character x

let before = str.slice(0, x);

Get the substring before the current character.

Iteration ↴

0 str.slice(0, 0);""

1 str.slice(0, 1);"A"

2 str.slice(0, 2);"AB"

3 str.slice(0, 3);"ABC"

4 str.slice(0, 4);"ABCD"

Initialize a variable to hold the substring after the current character x

let after = str.slice(x + 1);

Get the substring after the current character.

Iteration ↴

0 str.slice(0 + 1);"BCDE"

1 str.slice(1 + 1);"CDE"

2 str.slice(2 + 1);"DE"

3 str.slice(3 + 1);"E"

4 str.slice(4 + 1);""


Find first non-repeating character in string.

const str = "AABBCDD";

for (let x = 0; x < str.length; x++) {}

let before = str.slice(0, x); before

let after = str.slice(x + 1); after

First iteration ...

before""

after"ABBCDD"

Check if the current character x is NOT in either before or after substrings.

!before.includes(str[x]) && !after.includes(str[x])

Iteration ↴

0 "A" not in "" or "ABBCDD" false

1 "A" not in "A" or "BBCDD" false

2 "B" not in "AA" or "BCDD" false

3 "B" not in "AAB" or "CDD" false

4 "C" not in "AABB" or "DD" true Loop terminates

Loop terminates if condition is true and returns the current character "C" the first non-repeating character in the string.

If the loop completes, it will return null because no condition will have returned true.


Find first non-repeating character in string.

const string1 = "AABBCDDE";

function firstNonRepeatedChar(str) {

for (let x = 0; x < str.length; x++) {

let before = str.slice(0, x);

let after = str.slice(x + 1);

if (!before.includes(str[x]) && !after.includes(str[x])) {

return str.charAt(x);

}

}

return null;

}

call function

firstNonRepeatedChar(string1); returns ↴

"C"

Find first non-repeating character