Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method named mode that returns the most frequently occurring element of

ID: 3622181 • Letter: W

Question

Write a method named mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value. For example, if the array passed contains the values {27, 15, 15, 11, 27}, your method should return 15.
Hint: You may wish to look at the tally program to get an idea how to solve this problem. Can you write a version of this method that does not rely on the elements being between 0 and 100?

Explanation / Answer

please rate - thanks


import java.util.*;
public class array
{public static void main(String[] args)
{

int i;
int nums[]={27, 15, 15, 11, 27};
System.out.println("The array");
for(i=0;i<nums.length;i++)
     System.out.print(nums[i]+" ");
System.out.println("mode= "+mode(nums));
}
public static int mode(int nums[])
{int i,max;
int []counts=new int[101];
for(i=0;i<101;i++)
     counts[i]=0;
      
for(i=0;i<nums.length;i++)
     counts[nums[i]]++;
max=counts[0];
for(i=1;i<101;i++)
    if(counts[i]>max)
          max=counts[i];
for(i=0;i<101;i++)
      if(counts[i]==max)
             return i;
return 0;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote