0

I created a program to find the mode. Then made it print the mode in brackets like "1 3 [5] 4 [5]" but when there is no mode in the array list it declares the first value as the mode, like "[1] 3 4 5". I don't want it to show brackets on first integer if there is no mode.

public static int mode(int[] array) {
    int mode = array[0];
    int maxCount = 0;
    for (int i = 0; i < array.length; i++) {
        int value = array[i];
        int count = 1;
        for (int j = 0; j < array.length; j++) {
            if (array[j] == value)
                count++;
            if (count > maxCount) {
                mode = value;
                maxCount = count;
            }
        }
    }
    return mode;
}

Then I print it this way:

int[] array = ...
int mode = mode(array);
boolean first = true;
for (int elt : array) {
    // print separator unless it's the first element
    if (first) {
        first = false;
    } else {
        System.out.print(' ');
    }
    if (elt == mode) {
        System.out.print(elt);
    } else {
        System.out.print('[');
        System.out.print(elt);
        System.out.print(']');
    }
}
System.out.println();
1
  • I don't think that your code produces this output. Please recheck if the posted code is correct (also check the if conditions). Commented Apr 5, 2016 at 2:25

4 Answers 4

1

Since your function mode() returns by default the initial element in the array as the mode by default, you cannot tell whether the element is the mode or is a case when there is no mode at all. So, you could make a slight change to the function to return 0 when there is no mode instead, then your code would end up like follows:

class TestMode
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int[] array = {1,3,2,4,5};
        int mode = mode(array);
        for (int e : array) {
            if ((mode!=0) && (e==mode)) {
                System.out.print ("["+e+"]");
            }
            else {
                System.out.print(e);
            }
            System.out.print(" ");
        }
    }

    public static int mode(int[] array) {
        int mode = array[0];
        int maxCount = 0;
        for (int i = 0; i < array.length; i++) {
            int value = array[i];
            int count = 0;
            for (int j = 0; j < array.length; j++) {
                if (array[j] == value) count++;
                if (count > maxCount) {
                    mode = value;
                    maxCount = count;
                    }
                }
        }
        if (maxCount > 1) {
            return mode;
        }
        return 0;
    }
}

EDIT: The following is a function that returns the true mode set:

public static Set<Integer> mode2(List<Integer> list) {
    int maxFrequency = 0;
    boolean modeFound = false;
    Set<Integer> modeSet = new HashSet<>();
    Collections.sort(list);
    for (int i=0; i<list.size(); i++) {
        int number = list.get(i);
        int count = 1;
        for (; (i+count)<list.size() && list.get(i+count)==number; count++) {}
        i+=(count-1);
        if (maxFrequency!=0 && count!=maxFrequency) {
            modeFound = true;
        }
        if (count > maxFrequency) {
            modeSet.clear();
            modeSet.add (number);
            maxFrequency = count;
        }
        else if (count == maxFrequency) {
            modeSet.add(number);
        }
    }
    if (!modeFound) {
        modeSet.clear();
    }
    return modeSet;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Actually, I found a bug that was present in the original function. You need to set count=0 at line after "value=array[i]", and that was the reason the code was behaving like you said. Try running it now and see. One additional comment, looking at the mode definition, it looks like this would not satisfy the exact definition of mode anyway, since a set can have more than one mode when various numbers have the same greater frequency. For example, the mode of {1,1,3,4,5,2,2} would be {1,2} as both have freq=2; however {1,1,2,2,3,3} has no mode, as no number has a frequency greater than any other.
See the edit I made, it returns a set of numbers that are the mode for the input list.
Sorry but i cannot use that method as I have to use public static int mode(int[] array)
is there any other way i can show [1] [1] [2] 3 [2] were there are mulltimode
You can take converting my method to use int[] instead of Collections as a personal learning experience. Basically you can use an array instead of a set, and you can also sort arrays using Arrays.sort.
0

You need to be able to return a value that means 'no mode found' instead of just the first value. Here are three options (in decreasing elegance):

  1. If you are using Java 8, then you could return an Optional<Integer>. Then you could explicitly check if it has a value before using it.

  2. You could return an Integer instead of int and then use null to mean 'no value found'.

  3. Use a special value such as 0 or -1. This is what you are doing currently but it has the disadvantage that one of these numbers could well be the true mode of your sample.

Comments

0
boolean bool = true;
        int variable = 0;
        for (int i = 0; i < myIntArray.length; i++) {
            if (myIntArray[i] > 0) {
                if (variable == 0) {
                    variable = myIntArray[i];
                } else if (variable != myIntArray[i]) {
                    bool = false;
                    break;
                }
            }
        }
        if (bool == true) {
            System.out.println("                       ");
            System.out.println("No mode");
        } else {
            ArrayList<Integer> maximum = new ArrayList<Integer>();
            int maxNum = 0;
            for (int i = 0; i < myIntArray.length; i++) {
                if (myIntArray[i] > maxNum) {
                    maxNum = myIntArray[i];
                    maximum = new ArrayList<Integer>();
                    maximum.add(i);
                } else if (myIntArray[i] == maxNum) {
                    maximum.add(i);
                }
            }
            System.out.println("                       ");
            System.out.println("The maximum frequency, or number of occurrences, is " + maxNum + "  times for the number " + maximum);
            System.out.println("The mode is " + maximum);
        }

Comments

0

Use this algorithm when all the integers in the list are 0 and above. If not then initialize mode and temp with a number that is impossible to be present in the set.

        public static int getMode(List<Integer> list){
            int mode,freqMode,temp,tempFreq;
            mode=temp=-1;
            freqMode=tempFreq=0;
            Collections.sort(list);
            for(Integer integer:list){
                if(mode == -1){
                    mode = integer;
                    freqMode = 1;
                }else if(mode == integer){
                    freqMode++;
                }else if(temp != integer){
                    temp = integer;
                    tempFreq = 1;
                }else if(tempFreq >= freqMode){
                    mode = integer;
                    freqMode = tempFreq+1;
                    temp = -1;
                    tempFreq = 0;
                }else {
                    tempFreq++;
                }
            }
            return mode;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.