The Contains Duplicate problem is a coding interview question that involves checking if an array contains any duplicate elements. In this article, we will explore different approaches to solve this problem and implement the solution in Python.
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.
This approach has a time complexity of O(n) because we traverse the list containing n elements only once.
The approach involves using a dictionary to keep track of the elements we have seen so far. If we encounter an element that is already in the dictionary, we return true because it is a duplicate. If we traverse the entire array without finding any duplicates, we return false.
class Solution(object):
def containsDuplicate(self, nums):
seen_nums = {}
for i in range(len(nums)):
if nums[i] in seen_nums:
return True
else:
seen_nums[nums[i]] = nums[i]
return False