• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Formal Definition

A frequency array is a fixed-size integer array where each index represents a distinct element from a known, finite domain, and the value at that index stores the count of occurrences of that element in a given dataset. For lowercase English letters, int freq[26] uses indices 0–25 to represent ‘a’–‘z’.

freq[i]=count of character (i+’a’) in the input string\text{freq}[i] = \text{count of character } (i + \text{'a'}) \text{ in the input string}

Explanation

A frequency array is the simplest possible hash-like structure: instead of hashing keys, you use the key itself (after a trivial transformation) as the array index. When the domain is small and known (like 26 lowercase letters), this gives you O(1) access with zero hashing overhead, zero collision handling, and minimal memory. It is the go-to data structure for character frequency problems in competitive programming and technical interviews.

How It Works

  1. Declare an array of size equal to the domain size (e.g., int freq[26] = {0} for lowercase letters)
  2. Iterate through the input string character by character
  3. Convert each character to its corresponding index using ch - 'a'
  4. Increment freq[index] for each occurrence
  5. To query, iterate indices 0 through size-1 and check non-zero values

Mathematical Formulation

For a string SS of length nn over alphabet Σ\Sigma where Σ=k|\Sigma| = k:

freq[j]=i=1n[Si=σj]\text{freq}[j] = \sum_{i=1}^{n} [S_i = \sigma_j]

where [P][P] is the Iverson bracket (1 if PP true, 0 otherwise) and σj\sigma_j is the jj-th character of the alphabet.

Visual Explanation

frequency_array S Input: 'banana' LOOP Loop: for each char ch S->LOOP FREQ int freq[26] = {0} sized for a-z MAP index = ch - 'a' LOOP->MAP per character RESULT freq[0]=3, freq[1]=1, freq[13]=2 LOOP->RESULT after loop INC freq[index]++ MAP->INC INC->LOOP next character

Semantic Network

semantic_frequency_array THIS Frequency Array PRE1 Character-to-Index Mapping THIS--PRE1 built from PRE2 Array Data Structure THIS--PRE2 built from OUT1 Two-Phase Hashing THIS--OUT1 builds into OUT2 Most Frequent Character THIS--OUT2 builds into OUT3 Anagram Detection THIS--OUT3 builds into CON1 Unordered Map (hash map) THIS--CON1 contrasts with REL1 Direct Array Access THIS--REL1 related REL2 Known Range Assumption THIS--REL2 related

Key Properties

  • O(1) time for both insertion and lookup — true constant time, no amortization
  • Memory proportional to domain size (O(Σ)O(|\Sigma|)), not input size (O(n)O(n))
  • Zero hashing overhead — no hash function computation, no collision resolution
  • Only works when the domain is known, finite, and contiguous (or near-contiguous)
  • Access pattern is predictable — sequential memory access when iterating

Connections

Edge Cases & Gotchas

  • Forgetting to zero-initialize the array (int freq[26] = {0}) leads to garbage values
  • Using ch - 'a' on uppercase letters or non-alphabetic characters produces negative indices or out-of-bounds access
  • Array size must match the domain — freq[26] fails for extended ASCII or Unicode
  • Iterating all 26 slots when only 3 characters appeared wastes time (minor but relevant for sparse data)
  • The array stores frequencies, not positions — cannot directly answer “where does character X first appear?”