- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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

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)

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, nums): l = len(nums) temp = l*(l-1)/2 temp_sum = sum(nums) return temp_sum-temp ob = Solution() print(ob.solve([2, 1, 4, 3, 3]))

## Input

[2, 1, 4, 3, 3]

## Output

3

- Related Articles
- Program to find all missing numbers from 1 to N in Python
- Swift Program to Display All Prime Numbers from 1 to N
- Java Program to Display All Prime Numbers from 1 to N
- Kotlin Program to Display All Prime Numbers from 1 to N
- Haskell program to display all prime numbers from 1 to n
- Program to create a list with n elements from 1 to n in Python
- C Program to print numbers from 1 to N without using semicolon
- Find count of Almost Prime numbers from 1 to N in C++
- Find four missing numbers in an array containing elements from 1 to N in Python
- Program to find kth lexicographic sequence from 1 to n of size k Python
- Print prime numbers from 1 to N in reverse order
- An Interesting Method to Generate Binary Numbers from 1 to n?
- Compute sum of digits in all numbers from 1 to n
- Maximum XOR using K numbers from 1 to n in C++
- How to Display all Prime Numbers from 1 to N in Golang?

Advertisements