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

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

Elixir: Find First Non-Repeating Character

Elixir

Goal -- WPM

Ready
Exercise Algorithm Area
1defmodule StringAlgorithms do
2@moduledoc """
3Provides algorithms for string manipulation.
4"""
5
6@doc """
7Finds the first non-repeating character in a given string.
8
9It iterates through the string, counting character frequencies using a map.
10Then, it iterates again to find the first character with a count of 1.
11
12Examples:
13
14iex> StringAlgorithms.first_non_repeating_char("leetcode")
15"l"
16
17iex> StringAlgorithms.first_non_repeating_char("loveleetcode")
18"v"
19
20iex> StringAlgorithms.first_non_repeating_char("")
21nil
22
23iex> StringAlgorithms.first_non_repeating_char("aabb")
24nil
25"""
26def first_non_repeating_char(str) do
27# Handle empty string edge case immediately.
28if String.length(str) == 0 do
29nil
30else
31# Build a frequency map of characters.
32frequency_map = build_frequency_map(str)
33# Find the first character with a frequency of 1.
34find_first_unique(str, frequency_map)
35end
36end
37
38# Helper function to build the frequency map.
39defp build_frequency_map(str) do
40Enum.reduce(str, %{}, fn char, acc ->
41Map.update(acc, char, 1, fn count -> count + 1 end)
42end)
43end
44
45# Helper function to find the first character with a count of 1.
46defp find_first_unique(str, frequency_map) do
47Enum.find_value(str, fn char ->
48case Map.get(frequency_map, char) do
491 -> char
50_ -> nil
51end
52end)
53end
54end
Algorithm description viewbox

Elixir: Find First Non-Repeating Character

Algorithm description:

This Elixir function finds the first character in a string that does not repeat. It first constructs a map to store the frequency of each character. Then, it iterates through the string again, returning the first character whose frequency count is exactly one. This is useful in text processing tasks where identifying unique elements is important, such as in basic data validation or simple text analysis.

Algorithm explanation:

The `first_non_repeating_char` function first handles the edge case of an empty string by returning `nil`. For non-empty strings, it calls `build_frequency_map` to create a map where keys are characters and values are their counts. This map is built by iterating through the string once using `Enum.reduce`. The `build_frequency_map` helper uses `Map.update` to efficiently increment counts. Subsequently, `find_first_unique` iterates through the original string again. For each character, it checks its count in the `frequency_map`. If the count is 1, that character is returned immediately. If no such character is found after checking the entire string, `Enum.find_value` implicitly returns `nil`. The time complexity is O(N) because we iterate through the string twice, and map operations are O(1) on average. The space complexity is O(K), where K is the number of unique characters in the string, to store the frequency map.

Pseudocode:

FUNCTION first_non_repeating_char(string):
  IF string is empty THEN
    RETURN null
  END IF

  CREATE an empty map called frequency_map
  FOR EACH character in string:
    INCREMENT count for character in frequency_map
  END FOR

  FOR EACH character in string:
    IF frequency_map[character] is 1 THEN
      RETURN character
    END IF
  END FOR

  RETURN null
END FUNCTION