Find only one duplicate number in a million numbers

This puzzle was suggested to me recently in an adobe interview: There is an array containing millions of unordered positive numbers, where all the elements are different, except for one number that occurs exactly twice. The motive is to find that two times the number in the most optimal way.

PS Absolutely no order / pattern is applicable to the array.

The interviewer rejected any opportunity, as it would take a lot of time, he wanted to ask a question like a puzzle, and then offer a more reasonable solution.

+4
source share
3 answers

, , , . O(n log n) O(1).

, , , (/ ). , - .

, , (, ) : -)

, , . O(n) - :

dim array[several million] as zero
for each number:
    array[number]++
    if array[number] == 2:
        print number
        stop

32- ( 500 ) .

, , , . , .

+4

. O (n), HashSet. Hashing - .

25M , ~ 2 - O (n log n) - , . OTOH, , : -

BitMap (~ 1 ), ((0x7FFF_FFFF + 1)/8 - , ), . , - .

, . , Java. , .

public class Duplicate {
    public static void main(String[] args) throws Exception {
        Random r = new Random( 100L );
        int[] a = new int[25000000];
        Set<Integer> set  = new HashSet<>(a.length/2);
        boolean dupl = true;
        for( int i = 0; i < a.length; ){
            int x = Math.abs( r.nextInt() );
            if( set.add( x ) ){
                a[i++] = x;
            }
        }
        a[a.length-1] = a[0]; // Worst case for HashSet and BitSet
        set = null;

        System.out.println( "hash " + new Date() );
        set  = new HashSet<>();
        for( int i = 0; i < a.length; ++i ){
            if( ! set.add( a[i] ) ){
                System.out.println( a[i] );
                break;
            }
        }
        set = null;

        System.out.println( "bitmap " + new Date() );
        BitSet bs = new BitSet( 0x7FFF_FFFF ); 
        for( int i = 0; i < a.length; ++i ){
            if( bs.get( a[i]-1 ) ){
                System.out.println( a[i] );
                break;
            }
            bs.set( a[i]-1 );
        }

        System.out.println( "sort "  + new Date());
        Arrays.sort( a );
        for( int i = 1; i < a.length; ++ i ){
            if( a[i] == a[i-1] ){
                System.out.println( a[i] );
                break;
            }
        }
        System.out.println( "done " + new Date() );
    }
}

, Java 8 Arrays.sortParallel. , HW, . - , " ". , , , "" w.r.t. Java java.util.

+4

Since the data is unsorted, you need to check each number for the remaining (n-1), thus O (n ^ 2). They request an algorithm that has a time complexity of less than O (n ^ 2). To do this, you need either a tree or a hash table. If you sort this data and then apply some kind of algorithm, it will be a more time-consuming process. For the tree and hash table, you need O (n). Because they are best suited for organizing data and finding data.

0
source

Source: https://habr.com/ru/post/1611170/


All Articles