Leet Code
brian | Published: July 19, 2024, 6:01 p.m. | Updated: July 9, 2025, 9:41 p.m.
'''
Problem:
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.
'''
class Solution(object):
def is_duplicate(self, nums:list):
#first im creating an empty set
numbers = set()
#now we are going to loop over the list of numbers that will be passed in
for number in nums:
#next we check if the current number is in the set already
#if it IS, then we return True
if number in numbers:
return True
#if it is NOT, then we add that number to the set, and then continue until we finish looping through
#all the numbers, or until we find a duplicate
else:
numbers.add(number)
#if the execution reaches up to this point, then that means we DID NOT find any duplicate and so we will return False
return False
#the two is a duplicate, so it should return [True]
nums = [1, 2, 3, 4, 5]
solve = Solution()
print(solve.is_duplicate(nums))