Here is the solution to the Contains Duplicate Leetcode problem.

Given an array of integers num, return true if a value occurs at least twice in the array and return false if each 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

Constraints:

1 <= nums.length <= 105

-109 <= nums[i] <= 109

Solution in C# Language

In this approach, we will use sort (to correct for duplicates) and then we can run to check for the same. If there are duplicates, return true; otherwise, return false.

Here is the Space and Time Complexity for the solution.

Space Complexity:

Time Complexity: O(n)

Solution in Python Language

We know that the set data structure is defined to contain only unique data. But the list can contain duplicate content. So, if we convert a list to a set, its size will decrease if there are duplicate elements. By matching the length, we can solve this problem.

Here is the Space and Time Complexity for the solution.

Space Complexity:

Time Complexity: O(n)

Solution in JavaScript Language

In this approach, we will use sort (to correct for duplicates) and then we can run to check for the same. If there are duplicates, return true; otherwise, return false.

Here is the Space and Time Complexity for the solution.

Space Complexity:

Time Complexity: O(n)

Share This