Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to determine if the string has all unique characters using C#?
To determine if a string has all unique characters, you need to check if any character appears more than once. This can be accomplished using several approaches in C#, from simple nested loops to more efficient data structures like HashSet.
Using Nested Loop Approach
The basic approach involves comparing each character with every other character in the string −
using System;
class Program {
public static bool HasUniqueCharacters(string val) {
for (int i = 0; i
The output of the above code is −
'hello' has unique characters: False
'world' has unique characters: True
'unique' has unique characters: False
Using HashSet for Efficient Checking
A more efficient approach uses a HashSet to track seen characters. This method has O(n) time complexity compared to O(n²) for the nested loop −
using System;
using System.Collections.Generic;
class Program {
public static bool HasUniqueCharactersHashSet(string val) {
HashSet<char> seenChars = new HashSet<char>();
foreach (char c in val) {
if (seenChars.Contains(c))
return false;
seenChars.Add(c);
}
return true;
}
public static void Main() {
string[] testStrings = {"programming", "alphabet", "abc", "aabbcc"};
foreach (string str in testStrings) {
Console.WriteLine($"'{str}' has unique characters: {HasUniqueCharactersHashSet(str)}");
}
}
}
The output of the above code is −
'programming' has unique characters: False
'alphabet' has unique characters: False
'abc' has unique characters: True
'aabbcc' has unique characters: False
Using LINQ Approach
LINQ provides a concise way to check for unique characters by comparing the original length with the distinct character count −
using System;
using System.Linq;
class Program {
public static bool HasUniqueCharactersLinq(string val) {
return val.Distinct().Count() == val.Length;
}
public static void Main() {
string[] testCases = {"test", "abcd", "hello", "C#"};
foreach (string str in testCases) {
Console.WriteLine($"'{str}' has unique characters: {HasUniqueCharactersLinq(str)}");
}
}
}
The output of the above code is −
'test' has unique characters: False
'abcd' has unique characters: True
'hello' has unique characters: False
'C#' has unique characters: True
Performance Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Nested Loop | O(n²) | O(1) | Small strings, minimal memory usage |
| HashSet | O(n) | O(n) | Large strings, best performance |
| LINQ | O(n) | O(n) | Readable code, one-liner solution |
Conclusion
There are multiple ways to check if a string has unique characters in C#. The HashSet approach offers the best performance for larger strings with O(n) complexity, while the nested loop approach uses minimal memory. Choose the method based on your specific requirements for performance and readability.
