Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active May 28, 2018 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/49307528e2f5bd173800 to your computer and use it in GitHub Desktop.
Save thmain/49307528e2f5bd173800 to your computer and use it in GitHub Desktop.
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