Write a mode method in Java to find the most frequently occurring element in an array

前端 未结 14 2529
清酒与你
清酒与你 2020-11-29 07:17

The question goes:

Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at le

14条回答
  •  星月不相逢
    2020-11-29 07:22

    I have recently made a program that computes a few different stats, including mode. While the coding may be rudimentary, it works for any array of ints, and could be modified to be doubles, floats, etc. The modification to the array is based on deleting indexes in the array that are not the final mode value(s). This allows you to show all modes (if there are multiple) as well as have the amount of occurrences (last item in modes array). The code below is the getMode method as well as the deleteValueIndex method needed to run this code

    import java.io.File;
    import java.util.Scanner;
    import java.io.PrintStream;
    
    public static int[] getMode(final int[] array) {           
      int[] numOfVals = new int[array.length];
      int[] valsList = new int[array.length];
    
      //initialize the numOfVals and valsList
    
      for(int ix = 0; ix < array.length; ix++) {
         valsList[ix] = array[ix];
      }
    
      for(int ix = 0; ix < numOfVals.length; ix++) {
         numOfVals[ix] = 1;
      }
    
      //freq table of items in valsList
    
      for(int ix = 0; ix < valsList.length - 1; ix++) {
         for(int ix2 = ix + 1; ix2 < valsList.length; ix2++) {
            if(valsList[ix2] == valsList[ix]) {
               numOfVals[ix] += 1;
            }
         }
      }
    
      //deletes index from valsList and numOfVals if a duplicate is found in valsList
    
      for(int ix = 0; ix < valsList.length - 1; ix++) {   
         for(int ix2 = ix + 1; ix2 < valsList.length; ix2++) {
            if(valsList[ix2] == valsList[ix]) {
               valsList = deleteValIndex(valsList, ix2);
               numOfVals = deleteValIndex(numOfVals, ix2);
            }
         }
      }
    
      //finds the highest occurence in numOfVals and sets it to most
    
      int most = 0;
    
      for(int ix = 0; ix < valsList.length; ix++) {
         if(numOfVals[ix] > most) {
            most = numOfVals[ix];
         }
      }
    
      //deletes index from valsList and numOfVals if corresponding index in numOfVals is less than most
    
      for(int ix = 0; ix < numOfVals.length; ix++) {
         if(numOfVals[ix] < most) {
            valsList = deleteValIndex(valsList, ix);
            numOfVals = deleteValIndex(numOfVals, ix);
            ix--;
         }
      }
    
      //sets modes equal to valsList, with the last index being most(the highest occurence)
    
      int[] modes = new int[valsList.length + 1];
    
      for(int ix = 0; ix < valsList.length; ix++) {
         modes[ix] = valsList[ix];
      }
    
      modes[modes.length - 1] = most;
    
      return modes;
    
    }
    
    public static int[] deleteValIndex(int[] array, final int index) {   
      int[] temp = new int[array.length - 1];
      int tempix = 0;
    
      //checks if index is in array
    
      if(index >= array.length) {
         System.out.println("I'm sorry, there are not that many items in this list.");
         return array;
      }
    
      //deletes index if in array
    
      for(int ix = 0; ix < array.length; ix++) {
         if(ix != index) {
            temp[tempix] = array[ix];
            tempix++;
         }
      }
      return temp;
    }
    

提交回复
热议问题