Problem and Constraints

If the array contains any duplicate, return true. Else false.

All Approaches and Explanations in English

O(n^2) solution. O(1) space complexity

Double loop. And check if the elements are equals.

Bad. Solution.

O(n) time complexity. O(n) space complexity

Create a set and put items in one by one while checking if num is already there.

Code, if any

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> visited = new HashSet<>();
        for(int num: nums){
                return true;
        return false;