title: K Th Distinct Or Non Repeating Element In An Array 1
signature: func kThDistinctOrNonRepeatingElementInAnArray1(arr []int, size int, kthindex 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, size int, kthindex int, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(arr, size, kthindex),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver([]int{17, 25, 27, 27, 73, 91}, 
  	5, 
  	3, 73)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver([]int{-86, -74, -88, 28, -32, 20, -34, 32}, 
  	5, 
  	6, -1)
      fmt.Printf("TEST-1...%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}, 
  	33, 
  	32, -1)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver([]int{5, 11, 36, 27, 6, 24, 58, 44, 14, 68}, 
  	7, 
  	5, 6)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver([]int{-98, -98, -94, -92, -86, -86, -70, -66, -64, -64, -58, -52, -46, -44, -44, -38, -38, -28, -24, -12, -10, -4, -2, 2, 8, 10, 12, 20, 22, 26, 26, 36, 42, 52, 54, 60, 60, 68, 82, 82, 92, 98}, 
  	27, 
  	27, -1)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver([]int{0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1}, 
  	27, 
  	20, -1)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver([]int{3, 8, 9, 10, 10, 13, 14, 16, 18, 23, 24, 25, 27, 28, 30, 33, 36, 39, 42, 42, 44, 45, 45, 48, 52, 52, 55, 55, 59, 59, 59, 60, 61, 61, 66, 66, 67, 68, 71, 72, 75, 76, 79, 80, 94, 94}, 
  	44, 
  	26, 79)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver([]int{-12, 56, -48, 52, -96, -84, 32, -12, -6, 82, 70, 18, 66, -6, -22, -46, -54, 18, -14, -32, 68, 82, -44, -42, 10, 56, 8, -56, 24, 20, -38, 30, -52, -66, 82, -64, 68, -82, 52, -88, -34, -26, 94, 58, -4, -84, -60}, 
  	37, 
  	46, -1)
      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, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
  	22, 
  	21, -1)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver([]int{1, 23, 75, 84, 28, 34, 15, 13, 51, 69, 94, 45, 38, 38}, 
  	13, 
  	12, 45)
      fmt.Printf("TEST-9...%s\n", result)
      
          
  }
source_py: def k_th_distinct_or_non_repeating_element_in_an_array_1(arr, size, KthIndex):
      dict = {
          
      }
      vect = []
      for i in range(size):
          if (arr[i] in dict):
              dict[arr[i]] = (dict[arr[i]] + 1)
          else:
              dict[arr[i]] = 1
      for i in range(size):
          if (dict[arr[i]] > 1):
              continue
          else:
              KthIndex = (KthIndex - 1)
          if (KthIndex == 0):
              return arr[i]
      return (- 1)
source_cpp: int k_th_distinct_or_non_repeating_element_in_an_array_1(vector<int> arr, int n, int k) {
    unordered_map < int, int > h;
    for ( int i = 0;
    i < n;
    i ++ ) h [ arr [ i ] ] ++;
    if ( h . size ( ) < k ) return - 1;
    int dist_count = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( h [ arr [ i ] ] == 1 ) dist_count ++;
      if ( dist_count == k ) return arr [ i ];
    }
    return - 1;
  }