// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

public class Duplicate {
    public static Optional<Integer> find(int[] nums) {
        Set<Integer> found = new HashSet<>();
        for(int v : nums) {
            if(!found.add(v)) {
                return Optional.of(v);
            }
        }
        return Optional.empty();
    }
    public static void main(String[] args) {
        System.out.println(find(new int[] { 1, 3, 4, 2, 1 }));
        System.out.println(find(new int[] { 1, 3, 4, 2, 0 }));
        System.out.println(find(new int[] { }));
        System.out.println(find(new int[] { -1 }));
        System.out.println(find(new int[] { -1, -1 }));
    }
}
