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

前端 未结 14 2513
清酒与你
清酒与你 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:37

    check this.. Brief:Pick each element of array and compare it with all elements of the array, weather it is equal to the picked on or not.

      int popularity1 = 0;
      int popularity2 = 0;
      int popularity_item, array_item; //Array contains integer value. Make it String if array contains string value.
      for(int i =0;i= popularity2){
              popularity_item = array_item;
              popularity2 = popularity1;
          }
          popularity1 = 0;
      }
      //"popularity_item" contains the most repeted item in an array.
    

提交回复
热议问题