2

I am writing a method that would take the values and the array and find duplicates. If there are duplicates, like for instance two values have the same value, I will multiple that value by 2. If two values have the same value, I will multiple that value by 3. This will continue until if seven values are the same. I will multiple that value by 7.

This is my source code.

public static double calculateWinnings(int[]numbers)

    {
        double total = 0;
        for (int i = 0; i < numbers.length - 1; i++)
        {
            for (int j = i + 1; j < numbers.length; j++)
            {
                if(numbers[i] == numbers[j])
                {
                    total = numbers[i] * .01;
                    System.out.println("Total is " + total);
                    return total;
                }
            }
        }
        return total;

    }
hakre
  • 193,403
  • 52
  • 435
  • 836
  • What language are you writing it in? I'm assuming it's java because you used "public static" at the top... Hmm... – alexyorke Apr 09 '11 at 02:48
  • It is java. This have been puzzling me all day! – Tommy Lee Williams Apr 09 '11 at 02:49
  • I'm a still a little confused about what your trying to do. I'm using my logic, maybe it's my misunderstanding. – alexyorke Apr 09 '11 at 02:52
  • 1
    Your source code has very little relationship to the problem statement, which is self-contradictory anyway. FIrst you say that if two numbers match, you'll multiply them by 2, then you say by 3, and then your code multiplies a total (Which you don't mention in the problem statement) by .01. – Paul Tomblin Apr 09 '11 at 02:54
  • I am writing a lottery scratch off game. A random array will return some values. If two number match. I will multiple that particular number by 2. If three numbers match, I will multiple that particular number by 3 and so on. For example. If I match three 5s in the random generated array. I will multiply that 5 by three. I just can't write that code. I tried everything. Thanks for your time. – Tommy Lee Williams Apr 09 '11 at 02:58
  • What if there are multiple matches, e.g. three 5s and two 7s? What do you want to return from your function? – ejel Apr 09 '11 at 04:56

3 Answers3

1

If order doesn't matter, you should sort first, then analyze.

Sorting will put identical values next to each other, where you could more easily notice them in a for loop.

The Java Collections classes may also be of use here.

See for instance http://download.oracle.com/javase/tutorial/collections/intro/index.html

For instance, if you don't want to sort first and use a loop, you could use a HashMap from the collections classes.

HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
for(int i=0; i < numbers.length; ++i){
  Integer before = counts.get(numbers[i]);
  if (before == null) before=0;
  counts.put(numbers[i], before+1);
}

now you have a mapper from numbers to counts later you can use something like max(counts.valueSet()) to find the maximum count and then loop back through your hash to see which number caused that.

Paul
  • 26,170
  • 12
  • 85
  • 119
0

If you have same values at index 1, 4, 6, you will find them with

i j conclusion
--------------
1 4  2 values
1 6  3 values
4 6  4 values // oops! already counted

and so on. Well you would - but there is no so on, since you return on the first hit:

    if(numbers[i] == numbers[j])
    {
        total = numbers[i] * .01;
        System.out.println("Total is " + total);
        return total; // oops!
    }

Do you mean break?

user unknown
  • 35,537
  • 11
  • 75
  • 121
0

You should provide some example inputs and outputs. It's not clear exactly what output you expect. Are you just looking to find the most duplicates and then multiply that number by the frequency it appears? For example:

1 2 5 5 5 7 8 8 = three 5's = 15    

Or perhaps the two 8's wins because their total is 16? Or are you going to sum all of the duplicates? In any case I'd start with this, where MAX_NUM is the highest number you expect in the array:

int[] counts = new int[MAX_NUM];
for (int i = 0; i < numbers.length; i++) {
    counts[numbers[i]]++;
}

Now you have the counts of each number. If you're looking for the number with the highest count:

int num = 0;
int best = 0;
for (int i = 0; i < counts.length; i++) {
    if (counts[i] > best) {
        num = i;
        best = counts[i];
    }
}

Now num * best would be 15 for my example. Now num will contain the number that occurs the most and best will be the count for it. If there are two numbers with the same count then the higher number will win. Perhaps though in my example above you want 16 instead of 15 because the two 8's have a greater sum:

int max = 0;
for (int i = 0; i < counts.length; i++) {
    max = Math.max(i * counts[i], max);
}

Now max would have 16.

WhiteFang34
  • 70,765
  • 18
  • 106
  • 111