title: Maximum Distance Two Occurrences Element Array
signature: func maximumDistanceTwoOccurrencesElementArray(arr []int, n int) int {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int, expected int) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(arr []int, n int, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(arr, n),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver([]int{1, 20, 25, 28, 29, 31, 34, 35, 38, 39, 41, 43, 46, 55, 56, 60, 65, 66, 74, 77, 79, 80, 81, 83, 84, 88, 88, 88, 90, 91, 99}, 
  	27, 1)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver([]int{26, 14, 56, 84, -56, -84, -98, 12, -78, 18, -42, 58, 46, -66, -46, 66, 98, 34, -16, 8, -20, 66, 74, 26, 42, -84, 38, 86, 14, 86, 26, -42, -30, 6, -54, -76, -66, 18, 58, 66, 74, -62, 8, -42, 62, -14, -90, 98, -24}, 
  	34, 30)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver([]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
  	11, 10)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver([]int{69, 87}, 
  	1, 0)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver([]int{-98, -88, -86, -62, -52, -26, -24, -20, -12, 6, 8, 26, 36, 40, 42, 54, 68, 72, 84, 92, 94, 98, 98}, 
  	12, 0)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver([]int{1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0}, 
  	29, 28)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver([]int{11, 16, 17, 19, 20, 21, 21, 22, 27, 37, 45, 49, 64, 77, 81, 85, 96}, 
  	9, 1)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver([]int{-20, 0, 18, -96}, 
  	2, 0)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver([]int{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}, 
  	22, 20)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver([]int{54, 5, 98, 71, 9, 34, 60, 28, 58, 66, 28, 45, 4}, 
  	10, 0)
      fmt.Printf("TEST-9...%s\n", result)
      
          
  }
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;
  }