Find a special number in an array

后端 未结 9 1640
天命终不由人
天命终不由人 2020-12-25 09:07

There are many numbers in an array and each number appears three times excepting for one special number appearing once. Here is the question: how can I find the special numb

9条回答
  •  天涯浪人
    2020-12-25 09:35

    If the array is sorted, the problem is trivial, you just loop through the list, three items at a time, and check if the third item is the same as the current.

    If the array is not sorted, you can use a Hash Table to count the number of occurences of each numbers.

提交回复
热议问题