title: Frequent Element Array
signature: class Solution {
  
      public Integer frequentElementArray(ArrayList<Integer> arr, Integer n) {
test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(ArrayList<Integer> arr, Integer n, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(arr, n,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 1, 3, 11, 11, 11, 18, 20, 26, 26, 27, 30, 33, 39, 39, 42, 42, 48, 51, 51, 51, 51, 60, 66, 66, 68, 68, 69, 71, 72, 73, 76, 76, 77, 77, 77, 78, 90, 96)), 25, 51);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-74, -46, -38, -32, -22, -18, -14, -8, -4, 14, 30, 36, 42, 46, 48, 64, 66, 72, 80, 92, 94, 96, 96)), 18, -74);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 1)), 6, 0);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(3, 21, 23, 31, 31, 32, 45, 49, 52, 55, 56, 58, 67, 70, 71, 80, 87, 93, 96, 97)), 15, 31);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-98, -96, -84, -82, -72, -64, -62, -56, -52, -52, -48, -46, -42, -36, -32, -30, -30, -18, -16, -10, -2, 0, 6, 18, 22, 22, 40, 42, 50, 54, 64, 68, 68, 72, 80, 82, 84, 96)), 20, -52);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 29, 0);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(9, 12, 13, 28, 43, 46, 64, 66, 68, 89, 92)), 6, 9);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-94, -88, -82, -80, -72, -60, -56, -52, -46, -42, -42, -40, -40, -32, -32, -26, -24, -22, -22, -12, -12, -8, 4, 6, 18, 18, 22, 22, 32, 36, 36, 38, 40, 46, 48, 50, 54, 68, 76, 78, 80, 86, 86, 90, 92, 92, 94, 96)), 41, -42);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 1, 1, 1)), 4, 0);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(2, 4, 4, 5, 9, 16, 25, 29, 31, 32, 40, 41, 41, 43, 54, 61, 62, 63, 64, 65, 74, 74, 76, 80, 81, 81, 85, 86, 95, 96, 97, 97)), 16, 4);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(ArrayList<Integer> arr, Integer n, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(arr, n),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
source_py: def frequent_element_array(arr, n):
      arr.sort()
      max_count = 1
      res = arr[0]
      curr_count = 1
      for i in range(1, n):
          if (arr[i] == arr[(i - 1)]):
              curr_count += 1
          else:
              if (curr_count > max_count):
                  max_count = curr_count
                  res = arr[(i - 1)]
              curr_count = 1
      if (curr_count > max_count):
          max_count = curr_count
          res = arr[(n - 1)]
      return res
source_cpp: int frequent_element_array(vector<int> arr, int n) {
    sort(arr.begin(), arr.end());
    int max_count = 1, res = arr [ 0 ], curr_count = 1;
    for ( int i = 1;
    i < n;
    i ++ ) {
      if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++;
      else {
        if ( curr_count > max_count ) {
          max_count = curr_count;
          res = arr [ i - 1 ];
        }
        curr_count = 1;
      }
    }
    if ( curr_count > max_count ) {
      max_count = curr_count;
      res = arr [ n - 1 ];
    }
    return res;
  }