problemeasyalgorithmsgiven-numbers-from-1-to-n-1given numbers from 1 to n 1givennumbersfrom1ton1with-number-being-from-1-and-n-and-one-of-the-number-being-repeatative-find-the-repeating-elementwith number being from 1 and n and one of the number being repeatative find the repeating elementwithnumberbeingfrom1andnandoneofthenumberbeingrepeatativefindtherepeatingelement

Find duplicate number in array with 1 to N+1 numbers with one number repeating

EasyUpdated: Sep 29, 2025

Problem

Suppose you have an array of 1001 integers. The integers are in random order, but you know each of the integers is between 1 and 1000 (inclusive). In addition, each number appears only once in the array, except for one number, which occurs twice. Assume that you can access each element of the array only once. Describe an algorithm to find the repeated number. If you used auxiliary storage in your algorithm, can you find an algorithm that does not require it?

Examples

Example 1

Solution

Just add them all up, and subtract the total you would expect if only 1001 numbers were used from that.

Input: 1, 2, 3, 2, 4 => 12 as sum Expected: 1, 2, 3, 4 => 10 as sum Input - Expected => 2

Comments