using System;
using System.Collections.Generic;
class Program
{
public static int get_first_repeating_element(int[] arr) {
int x = -1;
HashSet<int> st = new HashSet<int>();
for (int i = arr.Length - 1; i >= 0; i--) {
if (st.Contains(arr[i])) {
x = i;
}
else {
st.Add(arr[i]);
}
}
if (x != -1)
return arr[x];
return -1;
}
static void Main()
{
int[] arr = new int[] {1, 2, 4, 5, 6, 5, 4, 3, 7};
int n = get_first_repeating_element(arr);
if (n != -1)
Console.WriteLine("First repeating element is: " + n);
else
Console.WriteLine("No repeating elements");
}
}
/*
run:
First repeating element is: 4
*/