Commons including duplicates in array elements in JavaScript

We need to write a JavaScript function that finds all common characters across all strings in an array, including duplicates. If a character appears multiple times in every string, it should appear that many times in the result.

Problem Statement

Given an array of strings, return an array of characters that appear in all strings. The frequency of each character in the result should match the minimum frequency of that character across all strings.

For example, with the input array ['door', 'floor', 'crook']:

  • 'o' appears 2 times in "door", 2 times in "floor", and 2 times in "crook" ? include 'o' twice
  • 'r' appears 1 time in each string ? include 'r' once

Example

const arr = ['door', 'floor', 'crook'];

const findCommon = (arr = []) => {
    if (arr.length === 0) return [];
    
    let prev = null;
    
    arr.forEach((str) => {
        const next = {};
        
        // Count characters in current string
        for (const char of str) {
            if (!prev) {
                // First string - count all characters
                next[char] = (next[char] || 0) + 1;
            } else if (prev[char]) {
                // Character exists in previous result - decrement and add to current
                prev[char] -= 1;
                next[char] = (next[char] || 0) + 1;
            }
        }
        
        prev = next;
    });
    
    // Convert character counts back to array
    const result = Object.keys(prev).reduce((acc, char) => {
        for (let i = 0; i < prev[char]; i++) {
            acc.push(char);
        }
        return acc;
    }, []);
    
    return result;
};

console.log(findCommon(arr));
[ 'r', 'o', 'o' ]

How It Works

The algorithm processes each string sequentially:

  1. First iteration: Count all characters in "door" ? {d: 1, o: 2, r: 1}
  2. Second iteration: For "floor", only keep characters that exist in previous result with available count
  3. Third iteration: Repeat the process with "crook"
  4. Final step: Convert the character counts back into an array format

Alternative Approach

Here's a clearer implementation using character frequency maps:

const findCommonAlternative = (strings) => {
    if (strings.length === 0) return [];
    
    // Get character frequency for each string
    const frequencies = strings.map(str => {
        const freq = {};
        for (const char of str) {
            freq[char] = (freq[char] || 0) + 1;
        }
        return freq;
    });
    
    const result = [];
    const firstStringChars = frequencies[0];
    
    // For each character in first string
    for (const char in firstStringChars) {
        // Find minimum frequency across all strings
        let minFreq = firstStringChars[char];
        
        for (let i = 1; i < frequencies.length; i++) {
            minFreq = Math.min(minFreq, frequencies[i][char] || 0);
        }
        
        // Add character minFreq times to result
        for (let i = 0; i < minFreq; i++) {
            result.push(char);
        }
    }
    
    return result;
};

const testArray = ['bella', 'label', 'roller'];
console.log(findCommonAlternative(testArray));
[ 'l', 'l', 'e' ]

Conclusion

Both approaches find common characters with their minimum frequencies across all strings. The alternative method is more readable, while the original is more memory-efficient for large datasets.

Updated on: 2026-03-15T23:19:00+05:30

168 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements