title: Maximum Distance Two Occurrences Element Array
signature: maximum_distance_two_occurrences_element_array <- function(arr, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(1L, 20L, 25L, 28L, 29L, 31L, 34L, 35L, 38L, 39L, 41L, 43L, 46L, 55L, 56L, 60L, 65L, 66L, 74L, 77L, 79L, 80L, 81L, 83L, 84L, 88L, 88L, 88L, 90L, 91L, 99L), 27L, 
          1L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(26L, 14L, 56L, 84L, -56L, -84L, -98L, 12L, -78L, 18L, -42L, 58L, 46L, -66L, -46L, 66L, 98L, 34L, -16L, 8L, -20L, 66L, 74L, 26L, 42L, -84L, 38L, 86L, 14L, 86L, 26L, -42L, -30L, 6L, -54L, -76L, -66L, 18L, 58L, 66L, 74L, -62L, 8L, -42L, 62L, -14L, -90L, 98L, -24L), 34L, 
          30L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 11L, 
          10L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(69L, 87L), 1L, 
          0L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-98L, -88L, -86L, -62L, -52L, -26L, -24L, -20L, -12L, 6L, 8L, 26L, 36L, 40L, 42L, 54L, 68L, 72L, 84L, 92L, 94L, 98L, 98L), 12L, 
          0L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L), 29L, 
          28L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(11L, 16L, 17L, 19L, 20L, 21L, 21L, 22L, 27L, 37L, 45L, 49L, 64L, 77L, 81L, 85L, 96L), 9L, 
          1L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-20L, 0L, 18L, -96L), 2L, 
          0L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 22L, 
          20L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(54L, 5L, 98L, 71L, 9L, 34L, 60L, 28L, 58L, 66L, 28L, 45L, 4L), 10L, 
          0L
      )))
  
  }
  
  main()
source_py: def maximum_distance_two_occurrences_element_array(arr, n):
      mp = {
          
      }
      maxDict = 0
      for i in range(n):
          if (arr[i] not in mp.keys()):
              mp[arr[i]] = i
          else:
              maxDict = max(maxDict, (i - mp[arr[i]]))
      return maxDict
source_cpp: int maximum_distance_two_occurrences_element_array(vector<int> arr, int n) {
    unordered_map < int, int > mp;
    int max_dist = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( mp . find ( arr [ i ] ) == mp . end ( ) ) mp [ arr [ i ] ] = i;
      else max_dist = max ( max_dist, i - mp [ arr [ i ] ] );
    }
    return max_dist;
  }