Find duplicate element in array in time O(n)

前端 未结 24 2987
余生分开走
余生分开走 2020-11-27 10:07

I have been asked this question in a job interview and I have been wondering about the right answer.

You have an array of numbers from 0 to n-1, one o

24条回答
  •  無奈伤痛
    2020-11-27 11:12

    int a[] = {2,1,2,3,4};
    
    int b[] = {0};
    
    for(int i = 0; i < a.size; i++)
    {
    
        if(a[i] == a[i+1])
        {
             //duplicate found
             //copy it to second array
            b[i] = a[i];
        }
    }
    

提交回复
热议问题