GAt Stripe, a service receives a list of integer event IDs and needs to quickly determine whether any ID appears more than once. A naive solution compares every pair of elements, but this is too slow for large inputs. Write an optimized algorithm to detect duplicates.
Implement a function contains_duplicate(nums) that takes a list of integers nums and returns a boolean:
True if any value appears at least twiceFalse if all values are distinctExample 1
nums = [1, 2, 3, 1]True1 appears twice.Example 2
nums = [4, 5, 6, 7]False1 <= len(nums) <= 10^5-10^9 <= nums[i] <= 10^9O(n^2) pairwise comparison approachYour solution should focus on performance optimization and clearly use a more efficient data structure or strategy than checking all pairs.
nums = [1, 2, 3, 1]OutputTrueWhyThe value `1` appears at indices 0 and 3, so the array contains a duplicate.nums = [4, 5, 6, 7]OutputFalseWhyNo value repeats, so all elements are distinct.nums = [0, -1, 2, -1]OutputTrueWhyThe value `-1` appears twice.1 <= len(nums) <= 10^5-10^9 <= nums[i] <= 10^9Input contains integers onlyTarget solution should improve on O(n^2) brute forcedef contains_duplicate(nums):