title: K Th Distinct Or Non Repeating Element In An Array 1
signature: int kThDistinctOrNonRepeatingElementInAnArray1(vector<int> arr, int size, int kthindex) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(vector<int> arr, int size, int kthindex, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(arr, size, kthindex), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver({17, 25, 27, 27, 73, 91}, 5, 3, 73); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver({-86, -74, -88, 28, -32, 20, -34, 32}, 5, 6, -1); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver({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); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver({5, 11, 36, 27, 6, 24, 58, 44, 14, 68}, 7, 5, 6); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver({-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); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver({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); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver({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); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver({-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); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver({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); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver({1, 23, 75, 84, 28, 34, 15, 13, 51, 69, 94, 45, 38, 38}, 13, 12, 45); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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;
  }