Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find duplicate element from n+1 numbers ranging from 1 to n in Python
Suppose we have a list of numbers called nums of length n + 1. These numbers are picked from range 1, 2, ..., n. As we know, using the pigeonhole principle, there must be a duplicate. We have to find that and return it.
So, if the input is like [2, 1, 4, 3, 3], then the output will be 3.
Approach
To solve this, we will follow these steps −
- l := size of nums
- temp := l*(l-1) /2
- temp_sum := sum of all elements in nums
- return (temp_sum - temp)
The logic is based on the mathematical formula. For numbers 1 to n, the sum is n*(n+1)/2. Since we have n+1 numbers where one is duplicate, the extra value is our duplicate number.
Example
Let us see the following implementation to get better understanding −
class Solution:
def solve(self, nums):
l = len(nums)
temp = l*(l-1)/2
temp_sum = sum(nums)
return int(temp_sum-temp)
ob = Solution()
print(ob.solve([2, 1, 4, 3, 3]))
The output of the above code is −
3
Alternative Method Using Set
We can also solve this problem using a set to track seen numbers ?
def find_duplicate(nums):
seen = set()
for num in nums:
if num in seen:
return num
seen.add(num)
numbers = [2, 1, 4, 3, 3]
print(find_duplicate(numbers))
3
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Mathematical Sum | O(n) | O(1) | Memory efficient |
| Set Tracking | O(n) | O(n) | Easy to understand |
Conclusion
The mathematical approach using sum formula is more memory-efficient with O(1) space complexity. The set-based approach is more intuitive but uses O(n) extra space to track seen numbers.
