Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicates of pairs in a list

I am familiar with the nub function on lists containing numbers, characters, or strings, but can someone explain to me how I can use the nub function from Data.List on a list of pairs?

Example:

[('a', 3),( 'b', 2),('a', 1),('b', 4)]

to

[('a', 3),('b', 2)]

As you can see, I want to remove all pairs where the key from the pair (key, value) is already in the list.

like image 869
sergeantSalty Avatar asked Dec 11 '22 08:12

sergeantSalty


1 Answers

Here's one way:

Prelude Data.List> nubBy (\(x,_) (x', _) -> x == x') [('a',1),('b',2),('b',3)]
[('a',1),('b',2)]
like image 176
גלעד ברקן Avatar answered Jan 04 '23 04:01

גלעד ברקן