El Capitan
El Capitan

Reputation: 27

Multiples of an element in a Java Array

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!

Upvotes: 0

Views: 1778

Answers (4)

Rock
Rock

Reputation: 11

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() );

Upvotes: 0

nhgrif
nhgrif

Reputation: 62052

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.

Upvotes: 0

arshajii
arshajii

Reputation: 129497

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.

Upvotes: 1

tbodt
tbodt

Reputation: 16987

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);
}

Upvotes: 0

Related Questions