Kellen Cole
Kellen Cole

Reputation: 27

Counting number of duplicate attributes of an ArrayList of objects of the same type

I am trying to make a class that evaluates each Poker player's two dealt cards and five table cards together and returns the number of duplicates found. For example if a player is dealt an Ace and a Two and the table is Ace, Three, Four, Ace, Five, it should return "Player 1 had three aces". This is what I have so far, it's accurate about fifty percent of the time, but sometimes it misses that the player had multiple pairs or miscounts how many duplicates there were. Here is an example console output

package edu.sbcc.cs105;

import java.util.*;

public class Evaluate {
    ArrayList<Card> sevenCards = new ArrayList<Card>();
    int player;
    String faces[] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King",
            "Ace" };

    public Evaluate(Card c1, Card c2, Card c3, Card c4, Card c5, Card c6, Card c7, int p) {
        sevenCards.clear();
        sevenCards.add(c1);
        sevenCards.add(c2);
        sevenCards.add(c3);
        sevenCards.add(c4);
        sevenCards.add(c5);
        sevenCards.add(c6);
        sevenCards.add(c7);
        this.player = p;
        duplicateFaces(sevenCards);
    }

    public void duplicateFaces(ArrayList<Card> cards) {
        int duplicateCount = 0;
        String card = "";
        ArrayList<String> dupCards = new ArrayList<String>();
        for (int i = 0; i < cards.size(); i++) {
            for (int j = i + 1; j < cards.size(); j++) {
                if ((cards.get(i).getFace()).equals(cards.get(j).getFace())) {
                    if (!dupCards.contains(cards.get(j).getFace())) {
                        duplicateCount++;
                        dupCards.add(cards.get(j).getFace());
                        card = cards.get(j).getFace();
                    }
                }
            }
            dupCards = new ArrayList<String>();
        }
        if (duplicateCount > 1) {
            returnDuplicates(duplicateCount, card);
        }
    }

    public void returnDuplicates(int count, String card) {
        System.out.println("Player " + player + " had " + count + " " + card + "s");
    }
}

Upvotes: 0

Views: 99

Answers (1)

sorifiend
sorifiend

Reputation: 6307

As mentioned in comments, you only have one instance of duplicateCount and card variables that are shared between all card faces, this will not work. Also this line if (!dupCards.contains(cards.get(j).getFace())) will only count duplicates the first time it finds them, but what about if you have three of the same card?

Here is a solution with comments that uses a Hashmap with the card face as the key, and the value as the count, this way you can easily track each card face with a single for loop and increment the count as required:

//Use a hashmap to store the cards
HashMap<String, Integer> results = new HashMap<>();

//Updated method to count duplicates
public void duplicateFaces(ArrayList<Card> cards) {
    //Use a single loop and update each card in the hashmap
    for (int i = 0; i < cards.size(); i++) {
        //Incriment count of duplicate card if found
        if (results.containsKey(cards.get(i).getFace())) {
            results.get(cards.get(i).getFace())++;
        }
        //Else enter new card with value of 1 (No duplicates yet)
        else {
            results.put(cards.get(i).getFace(), 1);
        }
    }
    //Now print the duplicates
    for (Map.Entry<String, Integer> entry : results.entrySet())
    {
        //Print out any cards that are found more than once
        if (entry.getValue() > 1){
            returnDuplicates(entry.getValue(), entry.getKey());
        }
    }
}

//Existing print method
public void returnDuplicates(int count, String card) {
    System.out.println("Player " + player + " had " + count + " " + card + "s");
}

Upvotes: 1

Related Questions