relude-0.7.0.0: Safe, performant, user-friendly and lightweight Haskell Standard Library
Copyright(c) 2016 Stephen Diehl
(c) 2016-2018 Serokell
(c) 2018-2020 Kowainik
LicenseMIT
MaintainerKowainik <xrom.xkov@gmail.com>
StabilityStable
PortabilityPortable
Safe HaskellSafe
LanguageHaskell2010

Relude.Nub

Description

Functions to remove duplicates from a list.

Performance

To check the performance there was done a bunch of benchmarks. Benchmarks were made on lists of Ints and Texts. There were two types of list to use:

  • Lists which consist of many different elements
  • Lists which consist of many same elements

Here are some recommendations for usage of particular functions based on benchmarking results.

  • hashNub is faster than ordNub when there're not so many different values in the list.
  • hashNub is the fastest with Text.
  • sortNub has better performance than ordNub but should be used when sorting is also needed.
  • unstableNub has better performance than hashNub but doesn't save the original order.
Synopsis

Documentation

hashNub :: forall a. (Eq a, Hashable a) => [a] -> [a] Source #

Like nub but runs in \( O(n \log_{16} n) \) time and requires Hashable.

>>> hashNub [3, 3, 3, 2, 2, -1, 1]
[3,2,-1,1]

ordNub :: forall a. Ord a => [a] -> [a] Source #

Like nub but runs in \( O(n \log n) \) time and requires Ord.

>>> ordNub [3, 3, 3, 2, 2, -1, 1]
[3,2,-1,1]

sortNub :: Ord a => [a] -> [a] Source #

Like ordNub runs in \( O(n \log n) \) but also sorts a list.

>>> sortNub [3, 3, 3, 2, 2, -1, 1]
[-1,1,2,3]

unstableNub :: (Eq a, Hashable a) => [a] -> [a] Source #

Like hashNub runs in \( O(n \log_{16} n) \) but has better performance; it doesn't save the order.

>>> unstableNub [3, 3, 3, 2, 2, -1, 1]
[1,2,3,-1]