Find three numbers appeared only once

后端 未结 6 1973
说谎
说谎 2020-12-08 03:15

In a sequence of length n, where n=2k+3, that is there are k unique numbers appeared twice and three numbers appeared only once.

The question is: how to fin

6条回答
  •  眼角桃花
    2020-12-08 03:52

    If a probabilistic solution will suffice then you could use a Bloom Filter.

    Create two Bloom filters. The first (A) contains numbers that have been found at least one, and the second (B) contains numbers that have been found twice.

    Pseudocode:

    A = empty
    B = empty
    
    foreach x in the list
      if x in A
        add x to B
      else
        add x to A
    
    foreach x in the list
      if x in A
        if !(x in B)
          print x
    

    If you use the full 1000KB then the probability of error would be ridiculously low.

提交回复
热议问题