Contains Duplicate LeetCode Solution

Last updated on July 15th, 2024 at 04:27 am

Here, We see Contains Duplicate Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

List of all LeetCode Solution

Topics

Array, Hash Table

Companies

Airbnb, Palantir, Yahoo

Level of Question

Easy

Contains Duplicate LeetCode Solution

Contains Duplicate LeetCode Solution

Problem Statement

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.

Example 1:
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

1. Contains Duplicate Leetcode Solution C++

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        if (nums.empty()) { return false; }
        unordered_map<int,int> mp;
        for (int i : nums) {
            if (++mp[i] > 1) {
                return true;
            }
        }
        return false;
    }
};

1.1 Explanation

  1. Initialization: An unordered map mp is used to store the frequency of each element in the vector nums.
  2. Check for Empty Input: If nums is empty, return false.
  3. Iterate and Count: Iterate through each element in nums:
    • Increment the count of the element in the map.
    • If the count becomes greater than 1, a duplicate is found, so return true.
  4. Return Result: If no duplicates are found after iterating through the vector, return false.

1.2 Time Complexity

  • Best Case: O(n) for iterating through the vector.
  • Average and Worst Case: O(n) due to hash map operations.

1.3 Space Complexity

  • Best Case: O(n) for storing all elements in the map.
  • Average and Worst Case: O(n) due to the size of the map.

2. Contains Duplicate Leetcode Solution Java

class Solution {
    public boolean containsDuplicate(int[] nums) {
        if(nums==null || nums.length==0)
            return false;
        HashSet<Integer> hset = new HashSet<Integer>();
        for(int idx: nums){
            if(!hset.add(idx)){
                return true;
            }
        }
        return false;
    }
}

2.1 Explanation

  1. Initialization: A HashSet is used to store unique elements from the array nums.
  2. Check for Null or Empty Input: If nums is null or empty, return false.
  3. Iterate and Add: Iterate through each element in nums:
    • Try to add the element to the set.
    • If the element is already present in the set (add method returns false), a duplicate is found, so return true.
  4. Return Result: If no duplicates are found after iterating through the array, return false.

2.2 Time Complexity

  • Best Case: O(n) for iterating through the array.
  • Average and Worst Case: O(n) due to hash set operations.

2.3 Space Complexity

  • Best Case: O(n) for storing all elements in the set.
  • Average and Worst Case: O(n) due to the size of the set.

3. Contains Duplicate Leetcode Solution JavaScript

var containsDuplicate = function(nums) {
    const s = new Set(nums);
return s.size !== nums.length
};

3.1 Explanation

  1. Initialization: A Set is created from the array nums, which automatically removes duplicate elements.
  2. Check for Duplicates: Compare the size of the set with the length of the array:
    • If the size of the set is not equal to the length of the array, duplicates were removed, so return true.
    • Otherwise, return false.

3.2 Time Complexity

  • Best Case: O(n) for iterating through the array to create the set.
  • Average and Worst Case: O(n) due to set operations.

3.3 Space Complexity

  • Best Case: O(n) for storing all elements in the set.
  • Average and Worst Case: O(n) due to the size of the set.

4. Contains Duplicate Leetcode Python

class Solution(object):
    def containsDuplicate(self, nums):
        return len(nums) != len(set(nums))

4.1 Explanation

  1. Initialization: A set is created from the list nums, which automatically removes duplicate elements.
  2. Check for Duplicates: Compare the length of the set with the length of the list:
    • If the length of the set is not equal to the length of the list, duplicates were removed, so return true.
    • Otherwise, return false.

4.2 Time Complexity

  • Best Case: O(n) for iterating through the list to create the set.
  • Average and Worst Case: O(n) due to set operations.

4.3 Space Complexity

  • Best Case: O(n) for storing all elements in the set.
  • Average and Worst Case: O(n) due to the size of the set.
Scroll to Top