November 29th, 2013, 02:34 PM

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 predefine 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.
November 29th, 2013, 02:55 PM

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.
November 29th, 2013, 04:45 PM

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
November 29th, 2013, 05:02 PM

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.
November 29th, 2013, 06:03 PM

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;
}
November 29th, 2013, 06:08 PM

You need to return an array in case there are more than one.
November 29th, 2013, 06:29 PM

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];
list.add(mode);
}
}
return list;
}