November 27th, 2013, 01:51 PM

Find multiple mode in array
I am trying to find mode inside an array. By mode, I mean to find any numbers inside my array that is being repeated. The following code works but I do not want to use HashSet.
Is there a way I could just use an array and loop through it to find the number of modes. I am only able to find a single mode without the use of a hashSet. Please advice. Thank you.
Code that returns correctly but uses HashSet:
Code:
import java.util.HashSet;
public class Mode {
public static void main(String[] args) {
int[] num = {2,3,4,7,7,7,8,8,8,8};
System.out.println(mode(num));
}
public static HashSet<Integer> mode(int[] num){
HashSet<Integer> list = new HashSet<>();
for(int x=0; x < num.length 1; x++){
for(int y=0; y < num.length; y++){
if(num[x] == num[y] && x != y){
list.add(num[y]);
}
}
}
return list;
}
}
Code that returns only one mode (The first mode found):
Code:
public class Mode {
public static void main(String[] args) {
int[] num = {2,3,4,5,8,8,8,7,7,7};
int mode = mode(num);
System.out.println(mode);
}
public static int mode(int[] num){
for(int x=0; x < num.length; x++){
for(int y=x+1; y < num.length; y++){
if(num[x] == num[y]){
return num[x];
}
}
}
return num[0];
}
}
November 27th, 2013, 02:32 PM

Can the array's contents be modified as it is searched?
November 27th, 2013, 02:59 PM

Hi,
Need the array contents to remain the same. Was thinking if it would be sensible to have a separate array to collect the mode information.
November 27th, 2013, 03:02 PM

How would the modes be reported? Perhaps an array of the numbers.
November 27th, 2013, 03:40 PM

Having problem with obtaining mutiple mode.
In terms of reporting the mode, I just plan to loop and print it close to below:
Code:
for(int x : newArray){
System.out.println(x);
}
November 27th, 2013, 03:47 PM

Sorry, I meant: how would the numbers that are repeats be saved so they could be returned to the caller. Your choice appears to be in an array.
Now you need to work on the logic to find and save those numbers.
November 27th, 2013, 04:33 PM

Sorry understood you wrongly in previous post.
Not sure if I am moving further away from the answer.. Attempted to save the mode numbers using ArrayList as follows. Problem is I want the answer to be just 7,8 based on this array but I end up getting 7,7,8,8,8..
Code:
import java.util.ArrayList;
public class Mode {
public static void main(String[] args) {
int[] num = {2,3,4,7,7,7,8,8,8,8};
ArrayList<Integer> list = new ArrayList<>();
for(int x=0; x < num.length 1; x++){
if(num[x] == num[x+1]){
list.add(num[x]);
}
}
System.out.println(list);
}
}
November 27th, 2013, 04:52 PM

Before adding, check if it is already there.
Do the repeated numbers need to be adjacent. Ie what about: 7,2,7?
November 27th, 2013, 05:15 PM

Hi,
Modified the code a little to do the check as follows:
Code:
if(!list.contains(num[x])){
list.add(num[x]);
}
It is working now. As for the need to be adjacent, I am sorting the array via: Arrays.sort() prior to checking for mode.
Do advice if I missed out anything or could have done it in a much shorter fashion. Thanks for guidance.
November 28th, 2013, 04:42 PM

I misunderstood what mode is. It is the single most common number in an array.
My method works if the scenario is such that different numbers repeats the same number of times for example : [7,7,8,8] so it returns both 7 and 8. But if the list contains [7,7,8,8,8], I should only be returning 8 which I am not currently doing. Stuck again.
November 28th, 2013, 05:11 PM

With the contents of the array being sorted, it shouldn't be too hard. Start at the end and work backwards.
November 28th, 2013, 05:38 PM

Sorry don't get it. Should I like add a counter for every possible repeated numbers followed by testing the counter value using if/else and see which is higher?
Even then, I don't see how I could predict the number of counters needed.
November 28th, 2013, 05:49 PM

You're right, that won't help.
predict the number of counters needed.
It shouldn't be larger than the size of the array.
November 29th, 2013, 12:09 PM

I was trying to do as below where I test every possible index in the list and add counters to count and compare the frequency. I didn't do the entire testing cos believe it is going to quite a lengthy test. Fairly certain this is wrong way. Do advice. Thank you.
Code:
public static void main(String[] args) {
int[] numbers = {1,2,3,4,4,4,5,8,8,8,8};
for(int x : mode(numbers)){
System.out.print(x + " ");
}
}
public static ArrayList<Integer> mode(int[] num){
ArrayList<Integer> list = new ArrayList<>();
for(int x=0; x < num.length1; x++){
if(num[x] == num[x+1]){
list.add(num[x]);
}
}
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
for(int x=0; x < list.size(); x++){
if(list.get(0) == list.get(1)){
counter1++;
}
else if(list.get(0) == list.get(1) && list.get(1) == list.get(2)){
counter1++;
}
else if(list.get(1) == list.get(2) && list.get(2) == list.get(3)){
counter2++;
}
}
return list;
}
November 29th, 2013, 12:48 PM

what is the desired results?
And why?
The variables: counterN look like they should be an array.
Why does the code use an ArrayList? Why not use the array directly?
Last edited by NormR; November 29th, 2013 at 12:51 PM.