TheAlgorithms/Java · error · IllegalArgumentException
Character array and frequency array must have the same lengt
Error message
Character array and frequency array must have the same length
What it means
Thrown by Huffman.buildHuffmanTree when charArray.length != charFreq.length. Each symbol must have exactly one corresponding frequency, so a mismatch means the arrays are misaligned and the loop that reads charFreq[i] for each charArray[i] would read out of bounds or pair the wrong values. The check runs after null and empty checks.
Source
Thrown at src/main/java/com/thealgorithms/others/Huffman.java:88
/**
* Builds a Huffman tree from the given character array and their frequencies.
*
* @param charArray array of characters
* @param charFreq array of frequencies corresponding to the characters
* @return root node of the Huffman tree
* @throws IllegalArgumentException if arrays are null, empty, or have different
* lengths
*/
public static HuffmanNode buildHuffmanTree(char[] charArray, int[] charFreq) {
if (charArray == null || charFreq == null) {
throw new IllegalArgumentException("Character array and frequency array cannot be null");
}
if (charArray.length == 0 || charFreq.length == 0) {
throw new IllegalArgumentException("Character array and frequency array cannot be empty");
}
if (charArray.length != charFreq.length) {
throw new IllegalArgumentException("Character array and frequency array must have the same length");
}
int n = charArray.length;
PriorityQueue<HuffmanNode> priorityQueue = new PriorityQueue<>(n, new HuffmanComparator());
// Create leaf nodes and add to priority queue
for (int i = 0; i < n; i++) {
if (charFreq[i] < 0) {
throw new IllegalArgumentException("Frequencies must be non-negative");
}
HuffmanNode node = new HuffmanNode(charArray[i], charFreq[i]);
priorityQueue.add(node);
}
// Build the Huffman tree
while (priorityQueue.size() > 1) {
HuffmanNode left = priorityQueue.poll();
HuffmanNode right = priorityQueue.poll();View on GitHub (pinned to fdfb9a395b)
Solutions
- Build symbols and frequencies from a single source (e.g., a Map<Character,Integer>) so they cannot diverge.
- After any mutation, re-derive both arrays together before the call.
- Log both lengths to catch divergence early.
Example fix
// before
char[] syms = ...; int[] frq = ...; // built separately, may diverge
HuffmanNode root = Huffman.buildHuffmanTree(syms, frq);
// after
Map<Character,Integer> table = buildFreqTable(input);
char[] syms = new char[table.size()];
int[] frq = new int[table.size()];
int i=0; for (var e : table.entrySet()) { syms[i]=e.getKey(); frq[i]=e.getValue(); i++; }
HuffmanNode root = Huffman.buildHuffmanTree(syms, frq); Defensive patterns
Strategy: validation
Validate before calling
if (charArray.length != charFreq.length) {
throw new IllegalStateException("symbols/frequencies length mismatch: " + charArray.length + " vs " + charFreq.length);
} Prevention
- Derive both arrays from a single Map so lengths stay in sync.
- Rebuild both arrays together after any mutation.
When it happens
Trigger: Building symbols and frequencies from two separate collections that drifted out of sync, removing an entry from one map but not the other, or a copy/transform that dropped an element from one array.
Common situations: Parallel collections maintained by hand, a partial update that modified one structure, or a deserialization that truncated one array.
Related errors
- Character array and frequency array cannot be null
- Character array and frequency array cannot be empty
- Frequencies must be non-negative
- Huffman tree is empty.
- Character '%c' (U+%04X) not found in Huffman dictionary.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/6df778df0b57fcd8.
Report an issue: GitHub.