import java.util.HashSet;
class Main {
public static void main(String[] args) {
printFirstRecurringNumber(new int[] {1,2,3,4,5,2,6,7,6});
}
public static void printFirstRecurringNumber(int[] arr){
if(arr != null && arr.length > 1){
HashSet<Integer> set = new HashSet<>();
for(int i = 0; i < arr.length; i++){
if(set.contains(arr[i])){
System.out.println(arr[i]);
return;
} else {
set.add(arr[i]);
}
}
}
throw new IllegalArgumentException("Supplied input is not valid or has no recurrences.");
}
}
import java.util.HashSet;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
printFirstRecurringNumber(new int[] {1,2,3,4,5,2,6,7,6});
}
public static void printFirstRecurringNumber(int[] arr){
if(arr != null && arr.length > 1){
final HashSet<Integer> set = new HashSet<>();
Arrays.stream(arr).forEach((int value) -> {
if(set.contains(value){
System.out.println(value);
return;
} else {
set.add(value);
}
});
}
throw new IllegalArgumentException("Supplied input is not valid or has no recurrences.");
}
}