[Haskell-cafe] Re: Function to detect duplicates

Daniel Fischer daniel.is.fischer at web.de
Tue Feb 23 07:28:03 EST 2010


Am Dienstag 23 Februar 2010 13:03:45 schrieb Ertugrul Soeylemez:
> Rafael Gustavo da Cunha Pereira Pinto <RafaelGCPP.Linux at gmail.com> wrote:
> > While solving a puzzle, I was posed the problem of finding if there
> > was no duplicates on a list.
> >
> > First I used:
> >
> > noneRepeated=null.(filter (>1)).(map length).group.sort
> >
> > But this seemed very unneficient, so I thought that I could detect the
> > duplicates while sorting, and devised this:
> >
> > import Control.Monad
> > import Data.Maybe
> >
> > noneRepeated=isNothing . (foldl merge (Just [])) . (map sort) . pairs
>
> import Data.List
>
> noneRepeated xs = xs == nub xs

Talk about inefficiency :)

import Data.Set (Set)
import qualified Data.Set as Set

noneRepeated = go 0 Set.empty
      where
        go ct st (x:xs)
            | Set.size st < ct = False
            | otherwise = go (ct+1) (Set.insert x st) xs
        go ct st [] = ct == Set.size st

>
>
> Greets
> Ertugrul



More information about the Haskell-Cafe mailing list