Determining full house in poker - JavaScript

A "full house" in poker is a hand containing three cards of one rank and two cards of another rank (e.g., three Kings and two Aces). We need to write a JavaScript function that checks if an array of five cards represents a full house.

Understanding Full House

A full house requires exactly:

  • Three cards of the same rank (three of a kind)
  • Two cards of another same rank (a pair)

Example Implementation

Here's a JavaScript function to detect a full house:

const arr1 = ['K', 'K', 'K', 'A', 'A']; // Full house: three Kings, two Aces
const arr2 = ['K', '2', 'K', 'A', 'J']; // Not a full house

const isFullHouse = arr => {
    const copy = arr.slice();
    for(let i = 0; i < arr.length; ){
        const el = copy.splice(i, 1)[0];
        if(copy.includes(el)){
            copy.splice(copy.indexOf(el), 1);
            if(copy.includes(el)){
                return true;
            }
        } else {
            i++;
        }
    }
    return false;
};

console.log(isFullHouse(arr1));
console.log(isFullHouse(arr2));
true
false

How the Algorithm Works

The function works by:

  1. Creating a copy of the input array
  2. For each card, removing it and checking if duplicates exist
  3. If a card appears at least three times, it returns true
  4. If no card appears three or more times, it returns false

Alternative Implementation Using Object Counting

Here's a clearer approach using frequency counting:

const isFullHouseAlt = arr => {
    const counts = {};
    
    // Count frequency of each card
    for(let card of arr) {
        counts[card] = (counts[card] || 0) + 1;
    }
    
    const frequencies = Object.values(counts).sort();
    
    // Full house has exactly frequencies [2, 3]
    return frequencies.length === 2 && 
           frequencies[0] === 2 && 
           frequencies[1] === 3;
};

console.log(isFullHouseAlt(['K', 'K', 'K', 'A', 'A'])); // true
console.log(isFullHouseAlt(['K', '2', 'K', 'A', 'J'])); // false
console.log(isFullHouseAlt(['Q', 'Q', 'Q', '7', '7'])); // true
true
false
true

Comparison

Method Readability Performance Accuracy
Splicing Method Complex O(n²) Good
Frequency Counting Clear O(n) Excellent

Conclusion

Both methods detect full house correctly, but frequency counting is more readable and efficient. The key is identifying exactly three cards of one rank and two of another.

Updated on: 2026-03-15T23:18:59+05:30

477 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements