Find a number where it appears exactly N/2 times

后端 未结 20 1869
旧巷少年郎
旧巷少年郎 2021-01-29 23:17

Here is one of my interview question. Given an array of N elements and where an element appears exactly N/2 times and the rest N/2 elements are unique

20条回答
  •  南方客
    南方客 (楼主)
    2021-01-29 23:58

    If I'm understanding the problem correctly: all we know about the array is it's length and it has (N/2)+1 unique elements, where 1 element is repeated N/2 times(in no specific order).

    I think this suffers a hard limit of O(N) for the solution as you can't really assert (for a generic array) that you've found the number without finding at least 2 of the same number. I dont think there exists a search for an unordered array that can detect a duplicate in O(logN) (please correct me if i'm wrong). You will always need to read at least N/2 +1 elements in the worst case.

提交回复
热议问题