Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

This method checks whether any value appears at least twice in an integer array using Stream functions. It uses a Set to track seen values -- anyMatch() short-circuits as soon as a duplicate is found.

Code Example:

public boolean containsDuplicate(int[] nums) {

    Set<Integer> seen = new HashSet<>();

    return Arrays.stream(nums)
            .anyMatch(num -> !seen.add(num));
}

Input:

nums = [1, 2, 3, 1]

Output:

true

Input:

nums = [1, 2, 3, 4]

Output:

false