Algorithm - How to delete duplicate elements in a list efficiently?

前端 未结 16 2147
执念已碎
执念已碎 2020-12-01 04:21

There is a list L. It contains elements of arbitrary type each. How to delete all duplicate elements in such list efficiently? ORDE

16条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 04:39

    It depends on what you mean by "efficently". The naive algorithm is O(n^2), and I assume what you actually mean is that you want something of lower order than that.

    As Maxim100 says, you can preserve the order by pairing the list with a series of numbers, use any algorithm you like, and then resort the remainder back into their original order. In Haskell it would look like this:

    superNub :: (Ord a) => [a] -> [a]
    superNub xs = map snd 
                  . sortBy (comparing fst) 
                  . map head . groupBy ((==) `on` snd) 
                  . sortBy (comparing snd) 
                  . zip [1..] $ xs
    

    Of course you need to import Data.List (sort), Data.Function (on) and Data.Ord (comparing). I could just recite the definitions of those functions, but what would be the point?

提交回复
热议问题