The simplest algorithm for poker hand evaluation
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A simple poker-hand evaluator does not need fancy lookup tables or bit tricks. For a five-card hand, the most direct algorithm is to count card ranks, check whether all suits match, check whether the ranks form a straight, and then classify the hand in descending order.
Represent the hand in a useful way
For a basic evaluator, it helps to represent each card as a rank and a suit. Then you can derive two structures:
- a frequency count of ranks
- a list of suits
Those two pieces of information are enough to identify all standard five-card hand categories.
The core classification logic
A straightforward evaluator usually checks these conditions in order:
- straight flush
- four of a kind
- full house
- flush
- straight
- three of a kind
- two pair
- one pair
- high card
The ordering matters because some features overlap. A straight flush is both a straight and a flush, but it should be classified at the highest matching category.
A runnable simple evaluator
Here is a compact five-card evaluator in Python.
This is not the fastest evaluator, but it is easy to read and correct for five-card hands.
Why counting works so well
Rank counts tell you almost everything about duplicate-based hands. For example:
- '
[4, 1]means four of a kind' - '
[3, 2]means full house' - '
[2, 2, 1]means two pair'
That makes the duplicate-based part of poker evaluation much simpler than many beginners expect.
Then you only need two additional checks:
- are all suits identical, which means flush
- are ranks consecutive, which means straight
Limits of the simple approach
A real poker engine often needs more than the category name. It also needs tie-breaking rules such as kicker comparison.
For example, two one-pair hands are not equal just because both are one pair. You still need to compare the pair rank and then the remaining cards in order.
The simple evaluator above stops at category classification. That is fine for learning and for toy programs, but not enough for full game resolution.
Common Pitfalls
A common mistake is forgetting the Ace-low straight, where A-2-3-4-5 counts as a straight. If you treat Ace only as high, that hand is misclassified.
Another issue is checking for flush or straight before checking the higher-ranking combination of straight flush. Classification order matters.
It is also easy to assume the same evaluator works unchanged for seven-card Texas Hold'em hands. Seven-card evaluation needs extra logic because you must choose the best five-card combination.
Summary
- The simplest five-card poker evaluator counts ranks and checks for flush and straight patterns.
- Evaluate categories from strongest to weakest so overlapping cases are handled correctly.
- Rank-frequency patterns make pair-based hands easy to identify.
- A simple classifier is fine for learning, but tie-breakers need extra logic.
- Seven-card poker evaluation is a different problem from basic five-card classification.
Related reading
- The time complexity of counting sort
- Three Way Merge Algorithms for Text
- Tickmark algorithm for a graph axis
- Tie breaking in a priority queue using python
- Time complexity analysis for finding the maximum element
- Time complexity deleting element of deque
- Time complexity for a very complicated recursion code
- Time complexity for Babylonian Method

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.