DevSupporter
DevSupporter

Welcome back

Please enter your details to sign in to your account

Forgot password?
Sign in

Memory Match

HashMapO(n)Beginner

Flip cards and find matching pairs. Built with HashMap logic.

โฑ Time
00:00
๐ŸŽฏ Moves
0
โญ Best
-

  • Click any card to flip it over
  • Click a second card to find its match
  • Matching pairs stay face up
  • Try to match all pairs in as few moves as possible
  • Your best score is saved automatically

1. Card Shuffling โ€” Fisher-Yates Algorithm

Cards are randomized using the Fisher-Yates shuffle, ensuring a perfectly uniform distribution.

function shuffle<T>(array: T[]): T[] {
  const arr = [...array];
  for (let i = arr.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [arr[i], arr[j]] = [arr[j], arr[i]];
  }
  return arr;
}
2. Match Detection โ€” HashMap O(1) lookup

When you flip a card, we store it in a Map keyed by its emoji value. Checking for a match is O(1) time complexity.

const cardMap = new Map<string, CardType>();

function checkMatch(card: CardType): boolean {
  if (cardMap.has(card.emoji)) {
    return cardMap.get(card.emoji)?.id !== card.id;
  }
  cardMap.set(card.emoji, card);
  return false;
}
3. Game State โ€” React useState

All game state is managed with React hooks, making the logic predictable and easy to follow.


2026 ยฉ DevSupporter - Playground for Developers by DevSupporter