{"record":{"id":"6df778df0b57fcd8","repo":"TheAlgorithms/Java","slug":"character-array-and-frequency-array-must-have-the","errorCode":null,"errorMessage":"Character array and frequency array must have the same length","messagePattern":"Character array and frequency array must have the same length","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/Huffman.java","lineNumber":88,"sourceCode":"\n    /**\n     * Builds a Huffman tree from the given character array and their frequencies.\n     *\n     * @param charArray array of characters\n     * @param charFreq  array of frequencies corresponding to the characters\n     * @return root node of the Huffman tree\n     * @throws IllegalArgumentException if arrays are null, empty, or have different\n     *                                  lengths\n     */\n    public static HuffmanNode buildHuffmanTree(char[] charArray, int[] charFreq) {\n        if (charArray == null || charFreq == null) {\n            throw new IllegalArgumentException(\"Character array and frequency array cannot be null\");\n        }\n        if (charArray.length == 0 || charFreq.length == 0) {\n            throw new IllegalArgumentException(\"Character array and frequency array cannot be empty\");\n        }\n        if (charArray.length != charFreq.length) {\n            throw new IllegalArgumentException(\"Character array and frequency array must have the same length\");\n        }\n\n        int n = charArray.length;\n        PriorityQueue<HuffmanNode> priorityQueue = new PriorityQueue<>(n, new HuffmanComparator());\n\n        // Create leaf nodes and add to priority queue\n        for (int i = 0; i < n; i++) {\n            if (charFreq[i] < 0) {\n                throw new IllegalArgumentException(\"Frequencies must be non-negative\");\n            }\n            HuffmanNode node = new HuffmanNode(charArray[i], charFreq[i]);\n            priorityQueue.add(node);\n        }\n\n        // Build the Huffman tree\n        while (priorityQueue.size() > 1) {\n            HuffmanNode left = priorityQueue.poll();\n            HuffmanNode right = priorityQueue.poll();","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/Huffman.java#L70-L106","documentation":"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.","triggerScenarios":"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.","commonSituations":"Parallel collections maintained by hand, a partial update that modified one structure, or a deserialization that truncated one array.","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."],"exampleFix":"// before\nchar[] syms = ...; int[] frq = ...; // built separately, may diverge\nHuffmanNode root = Huffman.buildHuffmanTree(syms, frq);\n\n// after\nMap<Character,Integer> table = buildFreqTable(input);\nchar[] syms = new char[table.size()];\nint[] frq = new int[table.size()];\nint i=0; for (var e : table.entrySet()) { syms[i]=e.getKey(); frq[i]=e.getValue(); i++; }\nHuffmanNode root = Huffman.buildHuffmanTree(syms, frq);","handlingStrategy":"validation","validationCode":"if (charArray.length != charFreq.length) {\n    throw new IllegalStateException(\"symbols/frequencies length mismatch: \" + charArray.length + \" vs \" + charFreq.length);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive both arrays from a single Map so lengths stay in sync.","Rebuild both arrays together after any mutation."],"tags":["huffman","precondition","length-mismatch","compression","tree"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}