Question

In Java how do I write a method called AnyType mode() that returns the most occurring...

In Java how do I write a method called AnyType mode() that returns the most occurring element in a list

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class ModeAnyType{
   public static void main(String[] args) {
       Object[] arr={1,3,3,4,3,3,5,6,10,12};
       Object[] arr1={1,3.5,3.5,4.2,3.5,3,5,6,10,12};
       Object[] arr2={'A','A','B','B','A','D'};
      
       System.out.println(getMode(arr));
       System.out.println(getMode(arr1));
       System.out.println(getMode(arr2));
   }
   //Method to read the mode of scores--------------------------------------------------
   public static Object getMode(Object arr[]) {
           Object maxValue=null;
           int maxCount = 0;

           for (int i = 0; i < arr.length; ++i) {
               int count = 0;
               for (int j = 0; j < arr.length; ++j) {
                   if (arr[j].equals(arr[i]))
                       ++count;
               }
               if (count > maxCount) {
                   maxCount = count;
                   maxValue = arr[i];
               }
           }

           return maxValue;
       }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
In Java how do I write a method called AnyType mode() that returns the most occurring...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT