check if all characters are unique
in a string
[ for-loop | object ]

Find if all characters are unique

Write a function that takes a string and returns true if all the characters in the string appear only once, otherwise returns false.


Example ...

Enter a string ...

"ABCDEFG" original string

The function will return true if all the characters in the string appear only once. All characters are unique.

The function will return false if any character in the string appears more than once. All characters are not unique.

Any quotes or empty spaces will be considered as valid characters.

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


Find if all characters are unique using ↴

Object → data structure used to store related data collections as key-value pairs.

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


Objects are a data structure used to store related data collections.

It stores data as key-value pairs, where each key is a unique identifier for the associated value.

Each key must be a string and must be unique, each value can be any data type.

If you define an object with duplicate keys, the last one will overwrite any preceding ones.

Find the value for any given key in the object.

const obj1 = {"A": 4, "B": 5, "C": 6 }; object

obj1["A"]; key "A"4

obj1["B"]; key "B"5

obj1["C"]; key "C"6

obj1["D"]; key "D"undefined key not found

Find the value for any given key in the object.

const obj2 = {"A": 4, "B": 5, "C": 6 }; object

const str = "ABC"; string

obj2[str[0]];4

obj2[str[1]];5

obj2[str[2]];6

obj2[str[3]];undefined key not found


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


Initialize a variable to hold the string to be checked to find if all characters are unique.

const string1 = "ABCDEFG"; → user input


Define a function allCharactersUnique to find if all characters are unique.

function allCharactersUnique(str) {}

The function takes a string as input str and checks if all characters in the string are unique by utilizing an object to keep track of character occurrences.

Initialize an object to keep track of character occurrences.

const charCount = {} charCount

Loop through each character in str

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

Get the current character str[x]

const char = str[x] char

Check if the current character char already exists in the charCount object.

if (charCount[char])

If true, the character already exists in the object.

Return false indicating not all characters are unique and end execution of the function.

return false

If the character is not found in the charCount object, it is unique,

add it to the charCount object.

charCount[char] = true

true value is not important. The existence of a character key is what is being looked for.

If no duplicates were found, return true (all characters are unique).

return true

If the function returns true, all characters are unique

If the function returns false, all characters are not unique


Call the function with ↴

allCharactersUnique(string1);


Find if all characters are unique.

const str = "ABCCD"; string with duplicate character

const charCount = {} empty object {}

Iterate through the string and check if the current character is in the object.

for (let x = 0; x < str.length; x++) loop through string

if (charCount[char]) → if current character char is in charCount object

charCount[char] = true → assign true value to char key in object

Iteration ↴

0 key : A {A: true} add to object

1 key : B {A: true, B: true} add to object

2 key : C {A: true, B: true, C: true} add to object

3 key : C duplicate character found, return false and end execution of function

If the loop completes without finding duplicates, the function returns true


Find if all characters are unique.

const string1 = "ABCDEFG";

function allCharactersUnique(str) {

const charCount = {};

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

const char = str[x];

if (charCount[char]) {

return false;

}

charCount[char] = true;

}

return true;

}

call function

allCharactersUnique(string1); returns ↴

true

Check if all characters are unique