### Thread: Find multiple mode in array

Page 2 of 2 First 12
1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
56
Rep Power
6
Desired result: define single most common number in an array. In the event 2 or more numbers appear the same numbers of times, I want to be able to return 2 or more numbers.

Based on previous codes, I am able to siphon out the repeated numbers using the ArrayList to store the repeated numbers. I used ArrayList as I was able to add and didn't need to determine the size of the array. Not sure how I could have added the numbers to another array without having to pre-define its length.

I am trying to see how I can count the frequency of numbers in the newly formed array. For example the following code for a list [7,7,8,8,8,].

Code:
```int counter;

if(num[x] == num[x+1]){
counter++
}```
It is not going to work cos it would ++ for the first 2 index, skip 3rd and carry on adding for 4th and 5th index. On top of the counter value being wrong, not sure how to relate back to the value in the list to determine which number frequents most. Confused and hope am not confusing you.
2. You need a way to associate a number with a count of its occurrences.

What kind of data structures are you allowed to use? A HashMap would be the simplest.
Or if you must, parallel arrays could do it. array1 has the number, array2 the count.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
56
Rep Power
6
I am not familiar with HashMap.

When you say parallel arrays, do you mean if I have a array of length 3, have a counter array of length 3 too to match as follows. One initialArray, second ArrayList1 to hold the repeated numbers, third counter Array to match the Arraylist index?

intialArray [1,1,2,3,3,3]

After siphoning out repeated numbers: ArrayList1 [1,3,3]

counterArray [count1,count2,count3]

If yes, I still don't get how to update the counter value and match the counter to actual numbers. Sorry
4. With parallel arrays, the first array would hold the number
the second array the count of occurrences of that number:
Given the array to search: [7,7,8,8,8,]
1st array: {7,8,,,,,}
2nd array: {2,3,,,,}

No arraylists used.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
56
Rep Power
6
Made some changes and now just using 2 arrays. Able to count but still will not work in the event there is 2 or more same repeated numbers ex: [1,1,3,3] is going to output 1. It is expected considering I am returning an int instead of an array...

Code:
```public static void main(String[] args) {
int[] n = new int[] {1,1,3,3,3};
System.out.println(mode(n));
}

public static int mode(int[] n) {
int mode = 0;
int maxCount = 0;

int[] counter = new int[5];

for (int x=0; x < n.length; x++) {
counter[n[x]]++;
if (maxCount < counter[n[x]]) {
maxCount = counter[n[x]];
mode = n[x];
}
}
return mode;
}```
6. You need to return an array in case there are more than one.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
56
Rep Power
6
Find myself reverting back to ArrayList which wouldn't work anyway. Think I'll give it a break for now.

Code:
```public static void main(String[] args) {
int[] n = new int[] {1,1,1,1,3,3,3,3};
System.out.println(mode(n));
}

public static ArrayList mode(int[] n) {
int mode = 0;
int maxCount = 0;

int[] counter = new int[5];
ArrayList<Integer> list = new ArrayList<>();

for (int x=0; x < n.length; x++) {
counter[n[x]]++;
System.out.println(counter[n[x]]);
if (maxCount < counter[n[x]]) {
maxCount = counter[n[x]];
mode = n[x];