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

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

T-SQL: Find First Non-Repeating Character

T-SQL

Goal -- WPM

Ready
Exercise Algorithm Area
1CREATE FUNCTION dbo.FindFirstNonRepeatingChar (@InputString NVARCHAR(MAX))
2RETURNS NVARCHAR(1)
3AS
4BEGIN
5DECLARE @CharCount TABLE (Char NCHAR(1), Count INT, FirstIndex INT);
6DECLARE @i INT = 1;
7DECLARE @StringLength INT = LEN(@InputString);
8DECLARE @Result NVARCHAR(1) = NULL;
9
10-- Handle empty or null input string
11IF @InputString IS NULL OR @StringLength = 0
12BEGIN
13RETURN NULL;
14END
15
16-- Populate CharCount table with character counts and first indices
17WHILE @i <= @StringLength
18BEGIN
19DECLARE @CurrentChar NCHAR(1) = SUBSTRING(@InputString, @i, 1);
20
21-- Check if character already exists in the table
22IF EXISTS (SELECT 1 FROM @CharCount WHERE Char = @CurrentChar)
23BEGIN
24UPDATE @CharCount
25SET Count = Count + 1
26WHERE Char = @CurrentChar;
27END
28ELSE
29BEGIN
30INSERT INTO @CharCount (Char, Count, FirstIndex)
31VALUES (@CurrentChar, 1, @i);
32END
33
34SET @i = @i + 1;
35END
36
37-- Find the character with count 1 and the minimum FirstIndex
38SELECT TOP 1 @Result = Char
39FROM @CharCount
40WHERE Count = 1
41ORDER BY FirstIndex ASC;
42
43-- If no non-repeating character is found, return NULL
44IF @Result IS NULL
45BEGIN
46RETURN NULL;
47END
48
49RETURN @Result;
50END
Algorithm description viewbox

T-SQL: Find First Non-Repeating Character

Algorithm description:

This T-SQL function identifies the first character in a string that appears only once. It iterates through the string, counts the occurrences of each character, and records the index of its first appearance. Finally, it returns the character that has a count of one and the smallest first index. This is useful for tasks like data validation or simple text processing where unique character identification is needed.

Algorithm explanation:

The function first initializes a table variable to store character counts and their first occurrence indices. It handles edge cases like null or empty input strings by returning NULL. The core logic iterates through the input string, updating the count for each character encountered and storing its first index if it's the first time seeing the character. After counting, it queries the table to find the character with a count of 1 and the minimum `FirstIndex`. This ensures that the *first* non-repeating character is returned. The time complexity is O(N) where N is the length of the string, as we iterate through the string twice (once to count, once implicitly in the final SELECT). The space complexity is O(K) where K is the number of unique characters in the string, for storing counts in the table variable. The invariant maintained is that `CharCount` accurately reflects the frequency and first seen index of characters up to the current iteration point. Correctness is ensured by the final `ORDER BY FirstIndex ASC` clause, guaranteeing the earliest occurring non-repeating character is selected.

Pseudocode:

FUNCTION FindFirstNonRepeatingChar(inputString):
  IF inputString is NULL or empty THEN RETURN NULL

  CREATE table charCounts (char, count, firstIndex)
  FOR i from 1 to length(inputString):
    currentChar = character at index i in inputString
    IF currentChar is in charCounts THEN
      increment count for currentChar
    ELSE
      insert (currentChar, 1, i) into charCounts

  SELECT TOP 1 char FROM charCounts WHERE count = 1 ORDER BY firstIndex ASC
  IF a character is found THEN RETURN it
  ELSE RETURN NULL