Search code examples
javaarraysreturn-valuemode

How to return multiple integers from a method of type int


As the title states, I'm attempting to return multiple integers from a method of type int (int mode(int [] a) . The method is designed to take in an array of integers and determine what the mode(s) would be. Another catch is that each value in the index of int [] a will range from 1-3. I cannot create objects, other methods to handle part of the solution or import anything to help me.

The problem I'm running into is if there are multiple modes in the array

ie. int [] array = {1,2,2,3,3,4,5,6}

As you can see, there's two modes in this case which are 2 and 3 and I'm not able to figure out how to return multiple integers from one method.

HERE ARE MY PROFESSORS INSTRUCTIONS"

"Write the method int mode(int []a). This function takes an array a indexed from 1 to a.length. Each cell of a contains either 1,2, or 3. Your function returns the value (1,2,3) that occurs most frequently in array a. You are expected to not use any imports(ie. Math, arraylists, etc) or change the return type. Assume that main only contains an array, and then a call to the mode method using the aforementioned array in main. Here is the template: "

int mode(int [] a){

}

Here's what my code looks like:

public static int mode(int [] a){

int value = 0;
int countOne= 0;
int countTwo = 0;
int countThr = 0;

for(int i =0;i<a.length;i++){
    int count = 0;
    for(int j=i+1;j<a.length;j++){
        if(a[i] == a[j]){
            if(a[i] == 1){
                countOne++;
            }
            if(a[i] == 2){
                countTwo++;
            }   
            if(a[i] == 3){
                countThr++;
            }
        }

    }
}
   if(countOne > countTwo && countOne > countThr)
       value = countOne;
   if(countTwo > countOne && countTwo > countThr){
       value = countTwo;
   }
   if(countThr > countOne && countThr > countTwo){
       value = countThr;
   }
   /* if(countThr == countTwo){
          return countThr, countTwo;
   if(countOne == countTwo){
          //return countOne, countTwo;
   if(countOne == countThree){
          return countOne, countThree;
      */       

   return value;

`

main{ int [] a = {1,2,2,3,3}; System.out.println(modeTwo(a));

Output: 3

While three is partially correct, since there a multiple modes in this case my desired output is

Desired Output: 2 3

In the mode method I'm just creating a counter for 1-3 to see how many times each occurs in the array. Then I set up conditionals to check which is greatest. I attempted to create conditions to check if they were equal as well, however it wouldn't work since I'd have to return two integers. I'm just completely lost. Any help would be appreciated.

int mode(int [] a){

}


Solution

  • EDIT

    Given your update about not being able to use ArrayLists or import other libraries, I made a custom class for counting occurrences Modes and am using just arrays to still count the max occurrences of each number, but only return the numbers in an int[] whose occurrences equal the max occurrences.

    Here's what I could come up with that will count all occurrences of each number in the array and return each number that has the max occurrences, which is the same as the mode.

       public static void main(String eth[]) {
            int[] numbers = new int[] {1, 2, 2, 3, 3, 4, 4, 5, 5, 10, 12, 33};
            int[] modes = mode(numbers);
    
            for (int i = 0; i < modes.length; i++) {
                if (modes[i] == -999) { // Stop printing once you reach the first sentinal value
                    continue;
                }
                System.out.println(modes[i]);
            }
        }
    
        private static int[] mode(int[] numbers) {
            Modes[] modes = new Modes[numbers.length];
            int modesIndex = 0;
    
            int maxOccurrence = 0;
            for (int i = 0; i < numbers.length; i++) {
                if (numbers[i] == -999) {
                    continue;
                }
    
                int number = numbers[i];
                numbers[i] = -999; // -999 is a sentinel value
    
                int count = 1;
                for (int j = i + 1; j < numbers.length; j++) {
                    if (numbers[j] == -999) {
                        continue;
                    }
    
                    if (numbers[j] == number) {
                        numbers[j] = -999;
                        count++;
                    }
                }
                if (count > maxOccurrence) {
                    maxOccurrence = count;
                }
                modes[modesIndex++] = new Modes(number, count);
            }
    
            int[] result = new int[numbers.length];
            for (int i = 0; i < result.length; i++) {
                result[i] = -999; // Sentinel value
            }
    
            int index = 0;
            for (int i = 0; i < modes.length; i++) {
                if (modes[i] == null) {
                    break; // Stop when you hit the first null
                }
                if (modes[i].Occurrences == maxOccurrence) {
                    result[index] = modes[i].Number;
                    index++;
                }
            }
    
            return result;
        }
    
        public static class Modes {
            public int Number;
            public int Occurrences;
    
            public Modes(int number, int occurrences) {
                Number = number;
                Occurrences = occurrences;
            }
        }
    

    Result:

    enter image description here