Last active
May 28, 2018 06:39
-
-
Save thmain/49307528e2f5bd173800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashSet; | |
public class FirstRepeatingelement { | |
public int find(int [] arrA){ | |
int index = -1; | |
HashSet<Integer> hs = new HashSet<>(); | |
for(int i = arrA.length-1;i>=0;i--){ | |
if(hs.contains(arrA[i])){ | |
index = i; | |
}else{ | |
hs.add(arrA[i]); | |
} | |
} | |
return arrA[index]; | |
} | |
public static void main(String args[]){ | |
int [] a = {1,2,5,7,5,3,10,2}; | |
FirstRepeatingelement f = new FirstRepeatingelement(); | |
System.out.println("{1,2,5,7,5,3,10,2}"); | |
System.out.println("first repeated element by index is : " + f.find(a)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment