The intent of this page is to collection the best program and help programmers to find good blog posts to read. Some of these blogs may not be written by Java developers, but Java developers should find it useful or interesting. Reading those blogs should be fun and often bring some fresh ideas

Sunday, April 12, 2020

Calculate Modal Value

Calculate Modal Value (100 Marks)

You will be given an array and you need to find the modal value.


Input Format

You will be taking a number as an input stdin which tells about the length of the array. On another line, array elements should be there with single space between them.

Constraints

1 <= L <= 10001 <= Ai <= 1000

Output Format
You need to print the modal value.

Sample TestCase 1
Input
8
6 3 9 6 6 5 9 3
Output
6
Solutions of above problem is here :













import java.io.*;
import java.util.*;

  public class CandidateCode {
        public static void Count(int arr[], int n){

       Map<Integer, Integer> map = new HashMap<>();

          for(int i=0; i<n; i++){
             if(map.containsKey(arr[i]))
                   map.put(arr[i], map.get(arr[i])+1);
            else
                  map.put(arr[i],1);
           }
      int max=(Collections.max(map.values()));
       for(Map.Entry<Integer, Integer> entry : map.entrySet())
          if(entry.getValue() == max)
               System.out.print(entry.getKey());
  }
 public static void main(String args[] ) throws Exception {

      Scanner sc = new Scanner(System.in);
      int size = sc.nextInt();
      int a[] = new int[size];

     for(int i=0; i<a.length; i++)
         a[i]=sc. nextInt();
        Count(a,size);
  }
}


No comments:

Post a Comment

If you have any doubt . Please let me know

String Operation

String Operation Using collection Framework Sorting Order  public static void sortAccending(String s) { char[] arr = s.toCharArray(); ...

Adbox