0

Writing a method in Java that looks through an ArrayList of objects (specifically "card" objects, that contain the number and suit of the card) that has been filled with random cards. The method returns a boolean value whether the ArrayList contains a duplicate.

So far I've tried a for loop within a for loop, but I didn't get very far.

Also, I need another method that returns the duplicate card, but I need to finish this first method first before I attempt that.

4
  • 2
    Post what you have so far... Commented Jan 15, 2014 at 2:25
  • Create a temporary copy of one of the lists then use removeAll passing in the second list, if the size changes, you have duplicate items... Commented Jan 15, 2014 at 2:26
  • y not sort them and compare one by one! Commented Jan 15, 2014 at 2:26
  • When you say duplicate, do you mean rank only? There are no cards with the same rank and suit in a standard deck. Commented Jan 15, 2014 at 2:27

3 Answers 3

3

This is how you would generally find duplicates:

public static bool findDuplicates(List<Card> listContainingDuplicates) {

        final Set<Card> set1 = new HashSet<Card>();

        for (Card yourCard : listContainingDuplicates) {
            if (!set1.add(yourCard)) {
                return true;
            }
        }
        return false;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Looking at this: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html you could just use indexOf and lastIndexOf.

In psuedo code:

myfunct(list, obj){
    if(list.indexOf(obj) == list.lastIndexOf(obj))
        return False;
    else
        return True; // Two or more ...
}

3 Comments

Method should check whether there's a duplicate.
ah my bad ... i go with hashmap then
I think you just need to put it in a loop and stop at the first true. I like the idea itself. It does not require extra space, though could be a hog on time.
0

If your algorithm is limited in space, sort the list and then find duplicated values in liner time. The total time complexity of this algorithm is O(nlogn).

public boolean hadDuplicatedCards(List<Card> cards) {
        Collections.sort(cards);
        for (int i = 0; i < cards.size() - 1; i++) {
            if(cards.get(i).equals(cards.get(i+1))) {
                return true;
            }
        }
        return false;
    }

Otherwise, new a HashSet, and add cards one by one, if an equal card already present in HashSet, add method will return true.

BTW, In the sort approach, your Card class need to implement Comparable interface, and don't forget to override the equals method. In the HashSet approach, you only need to override the equals method. But be aware that the HashSet approach needs extra O(n) space.

Thanks for PM 77-1's comments.

2 Comments

As OP states: "... "card" objects, that contain the number and suit of the card". So for your method to work Card needs to implement Comparable interface.
Nope. For HashSet approach we need just equals method, that we can reasonably expect a class to have. Do not need Comparable here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.