LC0287 - Find the duplicate in an array of N+1 integers (MEDIUM)#
This is the solution for Leetcode Problem 287 - Find the duplicate in an array of N+1 integers.
1"""Solution to Leetcode 287 - 287
2
3Link : https://leetcode.com/problems/find-the-duplicate-number/
4Level: medium
5"""
6
7
8class Solution:
9 def __init__(self, *args, **kwargs):
10 """Instantiates the solution object"""
11 self.args = args
12 self.kwargs = kwargs
13
14 def solve(self, *args, **kwargs):
15 """This implements the main solution"""
16 raise NotImplementedError("This solution is not yet implemented.")
17
18 def findDuplicate(self, nums: List[int]) -> int:
19 from collections import defaultdict
20
21 nums_dict = defaultdict(int)
22 for num in nums:
23 nums_dict[num] += 1
24 if nums_dict[num] == 2:
25 return num
26 return -1