Find duplicate element in array in time O(n)

前端 未结 24 2980
余生分开走
余生分开走 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 10:49

    I suggest using a BitSet. We know N is small enough for array indexing, so the BitSet will be of reasonable size.

    For each element of the array, check the bit corresponding to its value. If it is already set, that is the duplicate. If not, set the bit.

提交回复
热议问题