0

I have a pretty generic question about arrays. It is for a dice rolling game I have to make for homework.

I have an array of length 7 that contains ints that are the result of my various dice rolling methods and I need to find out if the array contains multiple instances of an element, what that element is, and how many times it occurs. Being new to Java, I don't even know where to look for a method that does this sort of thing. Does anyone know where I can find one, or better yet have tips on how to write one myself?

One of the hardest parts for me conceptually is that I don't know how many results I will have, as there could be up to two pairs and a trio. IE int[] roll = { 3, 3, 4, 4, 5, 5, 5 }. I think the best way to get around it would be to use a loop until my multiple finding method fails, each time removing the matching elements.

Thanks a lot for any help, this is due at midnight!

5
  • It depends, are the results always sorted? Commented Oct 30, 2013 at 0:46
  • @MadProgrammer Please spell "it" correctly while you can. Commented Oct 30, 2013 at 0:47
  • Are you rolling a single 6-sided die or a pair of 6-sided dice? (Will the results be 1-6 or 2-12?) Commented Oct 30, 2013 at 0:47
  • @tbodt I don't know, I depend on a lot of things ;) Commented Oct 30, 2013 at 0:50
  • Use a Map instead, with key being the dice, and value being number of occurrences. Commented Oct 30, 2013 at 0:54

4 Answers 4

1

The usual approach would be to use a Map to accumulate frequencies:

int[] roll = { 3, 3, 4, 4, 5, 5, 5 };

Map<Integer, Integer> counts = new HashMap<>(roll.length);

for (int a : roll)
    counts.put(a, counts.containsKey(a) ? counts.get(a) + 1 : 1);

for (Entry<?, Integer> e : counts.entrySet())
    if (e.getValue() > 1)
        System.out.println(e.getKey() + "\t" + e.getValue());
3   2
4   2
5   3

The first column is dice rolls and the second column is frequencies.

Sign up to request clarification or add additional context in comments.

2 Comments

This looks great, Thank you. Is there a way to access the numbers in the map? As in, assign the output to variables to be used later?
@user2812871 You can use counts.get(n) to get the frequency of number n. Also, if this answer helped you, you could consider accepting it.
0

You shouldn't be using an array for this. The best thing to use is a Map<Integer, Integer>. The keys are the numbers, the values are the number of occurrences. Here's example code to keep track of the numbers like this:

Map<Integer, Integer> numbers = new Map<Integer, Integer>();
public void addNumber(int n) {
    if (numbers.get(n) == null)
        numbers.set(n, 0);
    int count = numbers.get(n);
    numbers.set(n, count + 1);
}

public int countForNumber(int n) {
    if (numbers.get(n) == null)
        return 0;
    return numbers.get(n);
}

Comments

0
int[7] rollCounts = {0};
for(int i=0; i<roll.length; ++i)
    rollCounts[roll[i]]++;

Using this method, rollCounts[1] will contain the count of occurrences of 1 in roll[], and rollCounts[2] will contain the count of occurrences of 2 in roll[] etc. (rollCounts[0] should always be 0 assuming roll[i] never contains a 0, which it shouldn't).

Then you can just use some logic to check rollCounts[i] in a different for loop to do whatever you need to do based on the number of occurrences.

And honestly, you could just store your dice rolls in this array, unless it's important to keep track of the order the rolls occurred in.

Comments

0
int[] arr = { 2, 2, 4, 4, 4, 6, 6, 6, 6 };  // Can have any length.
Map<Integer, Integer> elemCount = new HashMap<Integer, Integer>();
Integer count = 0;
for ( int element : arr )
{
    count = elemCount.get ( (Integer) element );
    if ( count == null )
    {
        count = 1;
    }
    else 
    {
        count++;
    }
    elemCount.put ( (Integer) element , (Integer) count );
}
System.out.println ( elemCount.toString() );

Comments

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.