ℹ️ 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
2(
3@InputString NVARCHAR(MAX)
4)
5RETURNS NVARCHAR(1)
6AS
7BEGIN
8DECLARE @CharCounts TABLE
9(
10CharValue NVARCHAR(1),
11Count INT
12);
13
14DECLARE @i INT = 1;
15DECLARE @StringLength INT = LEN(@InputString);
16DECLARE @CurrentChar NVARCHAR(1);
17
18-- Handle empty string edge case
19IF @StringLength = 0
20BEGIN
21RETURN NULL;
22END
23
24-- Populate character counts
25WHILE @i <= @StringLength
26BEGIN
27SET @CurrentChar = SUBSTRING(@InputString, @i, 1);
28
29-- Check if character already exists in counts table
30IF EXISTS (SELECT 1 FROM @CharCounts WHERE CharValue = @CurrentChar)
31BEGIN
32UPDATE @CharCounts
33SET Count = Count + 1
34WHERE CharValue = @CurrentChar;
35END
36ELSE
37BEGIN
38INSERT INTO @CharCounts (CharValue, Count)
39VALUES (@CurrentChar, 1);
40END
41
42SET @i = @i + 1;
43END
44
45-- Find the first character with a count of 1
46SET @i = 1;
47WHILE @i <= @StringLength
48BEGIN
49SET @CurrentChar = SUBSTRING(@InputString, @i, 1);
50DECLARE @CharCount INT;
51
52SELECT @CharCount = Count
53FROM @CharCounts
54WHERE CharValue = @CurrentChar;
55
56IF @CharCount = 1
57BEGIN
58RETURN @CurrentChar;
59END
60
61SET @i = @i + 1;
62END
63
64-- Return NULL if no non-repeating character is found
65RETURN NULL;
66END
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 using a temporary table, and then iterates again to find the first character with a count of one. This is useful for data cleaning or simple text analysis where identifying unique elements is important.

Algorithm explanation:

The function first handles the edge case of an empty input string by returning NULL. It then iterates through the input string, using a `WHILE` loop and `SUBSTRING`, to populate a temporary table `@CharCounts` with each character and its frequency. A second `WHILE` loop iterates through the original string again. For each character, it checks its count in the `@CharCounts` table. If the count is exactly 1, that character is returned as the first non-repeating character. If the loop completes without finding such a character, it means all characters repeat, and NULL is returned. The time complexity is O(N) because we iterate through the string twice, and table lookups/updates are effectively constant time for a small alphabet. The space complexity is O(K), where K is the number of unique characters in the string, due to the temporary table.

Pseudocode:

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

  CREATE a temporary table `charCounts` with columns `charValue` and `count`

  FOR EACH character `currentChar` in `inputString`:
    IF `currentChar` is already in `charCounts` THEN
      INCREMENT `count` for `currentChar` in `charCounts`
    ELSE
      INSERT `currentChar` with `count` = 1 into `charCounts`
    END IF
  END FOR

  FOR EACH character `currentChar` in `inputString` (in order):
    GET `count` for `currentChar` from `charCounts`
    IF `count` is 1 THEN
      RETURN `currentChar`
    END IF
  END FOR

  RETURN NULL (no non-repeating character found)