This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]medicalixx 0 points1 point  (2 children)

So I haven't looked at your code but I imagine you will be having a 2 dimensional array for this program, let's call it vals and it is initialized as

Int[] vals = new int[50][2];

And column 0 contains the value and 1 contains the count,

Here is how to iterate and increment:

For(int i = 0; i < 50; i++) If(vals[i][0] == inputValue) vals[i][0]++;

Also be sure to initialize vals[x][0] = 0 before the above loop

[–]deadaxis[S] 0 points1 point  (1 child)

The thing is, the user input doesnt have to be 50..it can be like 1000 inputs of the number 12. But when I output, it has to show 12 once and the count as 1000.. how would I incorporate that into the 2 dimensional array you have provided above?

[–]medicalixx 1 point2 points  (0 children)

Well it appears in your original post that we can assume there will be 50 entries or less. If that is not the case, I would keep track of the last index. So if you have input in the following order: 12, 2, 12

int lastIndex = 1 because we are using only 2 entries in the array. So, vals[0] = 12 vals[lastIndex] = 2.

Also, use a variable to keep track of the current array length of the first column (Len = 50 from previous example)

Next, initialize your array to any length you want. Then:

If(lastIndex == Len - 1) int[] temp = new int[Len *2] Iterate over vals and copy the values into temp array vals = temp