The Blind 75 Leetcode Series: Contains Duplicate
Today, we are working on 217. Contains Duplicate
Given an integer array
nums
, returntrue
if any value appears at least twice in the array, and returnfalse
if every element is distinct.
This is a rather simple one. We are given a list of integers, and we just need to check if we see any duplicate in it.
Some questions to figure out before attempting this problem:
- how long is the list? Is there going to be a problem if I want to store it in memory?
…… that’s probably it honestly. This is a short and concise problem without too much vagueness.
If the interviewer tells us that we don’t have to worry about storing the entire list causing out-of-memory problem, then we can continue.
First approach is the brute-force, check-everything approach. We just want to find any duplicate, so we know that at some point in the list, two integers will be the same. Thus we create a double for-loop and check every element.
def containsDuplicate(nums]):
for i in range(len(nums) - 1):
for j in range(i+1, len(nums))…