```
Input: [1, 2, 2, 3, 4, 5, 6]
Output: 2
```

# find single repeating number from a big array

Carvia Tech | May 05, 2019 at 01:14 PM | 2 views | algorithm-datastructures

We have an array that contains large number of entries, all of them are unique except one that is repeating twice. We need to find that repeating number in a minimum time `O(n) time and O(1) space complexity`

## Sum of the Elements

We can sum up all the elements of input array and compare it with the below output:

\$1 + 2 + 3 + 4 + .. + n = (n * (n + 1)) / 2\$

The difference between actual array sum and math sum will give us the single duplicate number.

In Kotlin, we can write the below program to find the single duplicate number:

```
val array = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 2)
val sum = array.sum()
val mathSum = ((array.size -1) * array.size) / 2
val duplicateNumber = sum - mathSum
println(duplicateNumber)
```

2

###### Top articles in this category:

- Citibank Java developer interview questions
- BlackRock Top Java Interview Questions: Investment Banking Domain
- Cracking core java interviews - question bank
- Morgan Stanley Java Interview Questions
- Top 50 Multi-threading Java Interview Questions for Investment Banking Domain
- Sapient Global Market Java Interview Questions and Coding Exercise
- UBS Top 10 Java Interview Questions

###### Find more on this topic:

##### Java Interviews

Interview - Product Companies, eCommerce Companies, Investment Banking, Healthcare Industry, Service Companies and Startups.

Last updated 1 month ago

###### Recommended books for interview preparation:

##### Facebook Page

##### Similar Posts

- Use ExecutorCompletionService to compute results from 5 different datasources in parallel
- Sapient Global Markets important topics in Java
- Cracking Spring Microservices Interviews - question bank
- Cracking core java interviews - question bank
- What do you understand by Big O Notation
- Sapient Fee Calculator: Coding problem in Java
- find single repeating number from a big array
- How will you check if a given sentence is a pangram
- Explain Unix File Permissions
- What is difference between Primary key and Unique Key

Enter your email address to subscribe to this blog and receive notifications of new posts by email.