Here is the solution of Contains Duplicate leetcode problem.

Given as 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.

Solution in Python Language

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

Solution in TypeScript 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.

Share This