ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Find First Non-Repeating Character

C#

Goal -- WPM

Ready
Exercise Algorithm Area
1using System;
2using System.Collections.Generic;
3
4public class NonRepeatingCharFinder
5{
6public static int FindFirstNonRepeatingChar(string s)
7{
8// Handle edge case: null or empty string
9if (string.IsNullOrEmpty(s))
10{
11return -1; // Indicate no non-repeating character found
12}
13
14// Use a dictionary to store character counts
15Dictionary<char, int> charCounts = new Dictionary<char, int>();
16
17// First pass: count character frequencies
18foreach (char c in s)
19{
20if (charCounts.ContainsKey(c))
21{
22charCounts[c]++;
23}
24else
25{
26charCounts[c] = 1;
27}
28}
29
30// Second pass: find the first character with a count of 1
31for (int i = 0; i < s.Length; i++)
32{
33if (charCounts[s[i]] == 1)
34{
35return i; // Return the index of the first non-repeating character
36}
37}
38
39// If no non-repeating character is found after the second pass
40return -1;
41}
42
43public static void Main(string[] args)
44{
45string test1 = "leetcode";
46Console.WriteLine($"String: {test1}, First non-repeating index: {FindFirstNonRepeatingChar(test1)}"); // Expected: 0
47
48string test2 = "loveleetcode";
49Console.WriteLine($"String: {test2}, First non-repeating index: {FindFirstNonRepeatingChar(test2)}"); // Expected: 2
50
51string test3 = "aabb";
52Console.WriteLine($"String: {test3}, First non-repeating index: {FindFirstNonRepeatingChar(test3)}"); // Expected: -1
53
54string test4 = "";
55Console.WriteLine($"String: {test4}, First non-repeating index: {FindFirstNonRepeatingChar(test4)}"); // Expected: -1
56
57string test5 = "a";
58Console.WriteLine($"String: {test5}, First non-repeating index: {FindFirstNonRepeatingChar(test5)}"); // Expected: 0
59}
60}
Algorithm description viewbox

Find First Non-Repeating Character

Algorithm description:

This algorithm finds the index of the first character that appears only once in a given string. It first counts the frequency of each character using a hash map (dictionary). Then, it iterates through the string again, checking the count of each character. The index of the first character with a count of one is returned. This is useful in text processing and data analysis where identifying unique elements is important.

Algorithm explanation:

The `FindFirstNonRepeatingChar` method identifies the index of the first character that occurs exactly once in a string. It employs a two-pass approach. In the first pass, it iterates through the input string `s` and uses a `Dictionary<char, int>` to store the frequency of each character. If a character is encountered for the first time, its count is initialized to 1; otherwise, its existing count is incremented. The second pass iterates through the string again, from the beginning. For each character, it checks its count in the dictionary. If the count is 1, it means this is the first non-repeating character, and its index `i` is returned. If the loop completes without finding any character with a count of 1, it means all characters repeat or the string is empty, and -1 is returned. The time complexity is O(n) because the string is traversed twice, and dictionary operations (insertion, lookup) take average O(1) time. The space complexity is O(k), where k is the number of unique characters in the string, to store the character counts in the dictionary. Edge cases like null or empty strings are handled by returning -1.

Pseudocode:

function FindFirstNonRepeatingChar(string s):
  if s is null or empty:
    return -1

  create a map called charCounts to store character frequencies

  for each character c in s:
    increment count of c in charCounts

  for i from 0 to length of s - 1:
    if count of s[i] in charCounts is 1:
      return i

  return -1