Find a number where it appears exactly N/2 times

后端 未结 20 1856
旧巷少年郎
旧巷少年郎 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:57

    If you are told that the element you are looking for is the non-unique one surely the quickest way to do it is to iterate along the array until you find two the same and then return that element and stop looking. At most you have to search half the array.

    I think this is O(n) so I guess it doesn't really help.

    It seems too simple so I think I don't understand the problem correctly.

提交回复
热议问题