find first non-repeating character
in a string
[ for loop | indexOf | lastIndexOf ]

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


Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element at index 3 → undefined index not found


Find first non-repeating character in a string using ↴

for loop → executes a block of code a number of times.

indexOf() method → returns the first index at which a given element can be found in an array.

lastIndexOf() method → returns the last index at which a given element can be found in an array.


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


indexOf() method returns the first index at which a given element can be found in an array, or -1 if it is not present.

Find first index of element "C" in the array.

const arr2 = ["A", "B", "C", "A", "B", "C"];

arr2.indexOf("C"); returns ↴

2 → index of first occurrence of element "C"

Element is present.

Find first index of element "D" in the array.

const arr3 = ["A", "B", "C", "A", "B", "C"];

arr3.indexOf("D"); returns ↴

-1 → element "D" NOT found in array,

Element NOT present.

array.indexOf(element) !== -1

If result is NOT -1 then the element is present in the array.


lastIndexOf() method returns the last index of a specified value, or -1 if it is not present.

Find last index of element "B" in the array.

const arr4 = ["A", "B", "C", "A", "B", "C"];

arr4.lastIndexOf("B"); returns ↴

4 → index of last occurrence of element "B"

Element is present.

Find last index of element "D" in the array.

const arr5 = ["A", "B", "C", "A", "B", "C"];

arr5.lastIndexOf("D"); returns ↴

-1 → element "D" NOT found in array

Element NOT present.

array.lastIndexOf(element) !== -1

If result is NOT -1 then the element is present in the array.


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 current character.

const char = str[x] char

Check if char appears only once in the string str

if (str.indexOf(char) === str.lastIndexOf(char)) {}

Check if the returned value of str.indexOf(char) is the same as str.lastIndexOf(char)

If true, the index of the first character found is the same as the index of the last character found and therefore is a single unique character.

Return the first non-repeated character.

return char character found

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 = "AABBCDDE";

str.indexOf("A");0

str.lastIndexOf("A");1

str.indexOf("B");2

str.lastIndexOf("B");3

str.indexOf("C");4

str.lastIndexOf("C");4

Iteration ↴

0 A indexOf("A") === lastIndexOf("A") 0 === 1 false

1 A indexOf("A") === lastIndexOf("A") 0 === 1 false

2 B indexOf("B") === lastIndexOf("B") 2 === 3 false

3 B indexOf("B") === lastIndexOf("B") 2 === 3 false

4 C indexOf("C") === lastIndexOf("C") 4 === 4 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 char = str[x];

if (str.indexOf(char) === str.lastIndexOf(char)) {

return char;

}

}

return null;

}

call function

firstNonRepeatedChar(string1); returns ↴

"C"

Find first non-repeating character