Algorithm to find two repeated numbers in an array, without sorting

前端 未结 24 2083
南方客
南方客 2020-11-28 06:58

There is an array of size n (numbers are between 0 and n - 3) and only 2 numbers are repeated. Elements are placed randomly in the array.

E.g. in {2, 3, 6, 1, 5, 4

24条回答
  •  攒了一身酷
    2020-11-28 07:11

    Without sorting you're going to have a keep track of numbers you've already visited.

    in psuedocode this would basically be (done this way so I'm not just giving you the answer):

    for each number in the list
       if number not already in unique numbers list
          add it to the unique numbers list
       else
          return that number as it is a duplicate
       end if
    end for each
    

提交回复
热议问题