217.Contains Duplicate


LeetCode 217. Contains Duplicate [Easy]

  • 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.
Input: nums = [1,2,3,1]
Output: true

Example 2

Input: nums = [1,2,3,4]
Output: false

Example 3

Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true

Method 1

【O(n)time∣O(n)space】
package Leetcode.Array;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zhengstars
 * @date 2024/02/19
 */
public class ContainsDuplicate {
    public static boolean containsDuplicate(int[] nums) {
        // Create a new HashSet instance
        Set<Integer> set = new HashSet<Integer>();

        // Iterate over the given array
        for (int x : nums) {
            // Try to add each element to the set
            if (!set.add(x)) {
                // If set.add() returns false, it means the element already exists in the set (duplicate found)
                return true;
            }
        }
        // If no duplicate is found after the iteration, return false
        return false;
    }

    /**
     * The main function to test the solution
     */
    public static void main(String[] args) {

        // Test case 1. Expected output: true
        int[] nums1 = {1,2,3,1};
        System.out.println(containsDuplicate(nums1));

        // Test case 2. Expected output: false
        int[] nums2 = {1,2,3,4};
        System.out.println(containsDuplicate(nums2));

        // Test case 3. Expected output: true
        int[] nums3 = {1,1,1,3,3,4,3,2,4,2};
        System.out.println(containsDuplicate(nums3));
    }
}




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • 2379. Minimum Recolors to Get K Consecutive Black Blocks
  • 2471. Minimum Number of Operations to Sort a Binary Tree by Level
  • 1387. Sort Integers by The Power Value
  • 2090. K Radius Subarray Averages
  • 2545. Sort the Students by Their Kth Score