Find the first repeating element in an array of integers | C # program to find first repeating element in arr [ ] ; This function prints the first repeating element in arr [ ] ; Initialize index of first repeating element ; Creates an empty hashset ; Traverse the input array from right to left ; If element is already in hash set , update min ; Else add element to hash set ; Print the result ; Driver method to test above method
using System ; using System . Collections . Generic ; public class GFG { public static void printFirstRepeating ( int [ ] arr ) { int min = - 1 ; HashSet < int > set = new HashSet < int > ( ) ; for ( int i = arr . Length - 1 ; i >= 0 ; i -- ) { if ( set . Contains ( arr [ i ] ) ) { min = i ; } else { set . Add ( arr [ i ] ) ; } } if ( min != - 1 ) { Console . WriteLine ( " The ▁ first ▁ repeating ▁ element ▁ is ▁ " + arr [ min ] ) ; } else { Console . WriteLine ( " There ▁ are ▁ no ▁ repeating ▁ elements " ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; printFirstRepeating ( arr ) ; } }