How to determine if the string has all unique characters using C#?


To determine if a string has unique characters or not, firstly check a word in the string with the next word −

for (int j = i + 1; j < val.Length; j++) {
   if (val[i] == val[j])
}

If you find a match, that would mean the string do not have unique characters.

If you are unable to find a match, then the string has all unique characters.

In case of a match, return false i.e. unique characters not found −

for (int j = i + 1; j < val.Length; j++) {
   if (val[i] == val[j])
   return false;
}

Updated on: 20-Jun-2020

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements