Extracting Unique Elements From a List
From Erlang Community
| Revision as of 00:00, 22 November 2006 (edit) Kaiserpanda (Talk | contribs) m ← Previous diff |
Revision as of 08:02, 22 November 2006 (edit) (undo) 213.171.204.166 (Talk) (→Solution) Next diff → |
||
| Line 53: | Line 53: | ||
| [[Category:CookBook]][[Category:ListRecipes]] | [[Category:CookBook]][[Category:ListRecipes]] | ||
| - | |||
| - | |||
| - | |||
| - | [http://www.casino-theory.com/craps-rules/internet-craps-gambling.html internet craps gambling] | ||
| - | [http://www.bestweb-online-casinos.com/best-online-casino/online-casino-free-game.html online casino free game] | ||
| - | [http://www.web-craps.com craps] | ||
| - | [http://www.magical-casino.com/strategy.html Online casino strategies.] | ||
| - | [http://www.casino-theory.com/online-casino-royale/strategy-online-casino.html strategy online casino] | ||
| - | [http://www.casinos-go.com/online-casino-tips/online-casino-games.html online casino games] | ||
| - | [http://www.casino-theory.com/online-casino-royale/play-free-online-casino.html play free online casino] | ||
| - | [http://www.casino-theory.com/online-casino-royale/play-free-online-casino.html play free online casino] | ||
| - | [http://www.magical-casino.com/security.html Casinos security online.] | ||
| - | [http://www.gambling-online-theory.com/online-casino/bet-online-casino.html bet online casino] | ||
Revision as of 08:02, 22 November 2006
Problem
You want to eliminate duplicate values from a list.
Solution
There are several possible solutions for this problem. Here are some of these:
Using lists:usort
The lists module contains a wealth of list processing functionality. One possible solution to this problem is to use the lists:usort function, which takes a list and returns a sorted copy of the original list, with all duplicates removed:
1> UL = [1,2,8,7,8,10,3,12,3,99,188,3,2,1,3,5,15,72]. [1,2,8,7,8,10,3,12,3,99,188,3,2,1,3,5,15,72] 2> lists:usort(UL). [1,2,3,5,7,8,10,12,15,72,99,188] |
Using the sets Module
Erlang standard libraries includes a module, sets, with a variety of functions related to generating, creating, and manipulating mathematical sets.
10> Set = sets:from_list(UL).
{sets,12,
16,
16,
8,
80,
48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
{{[],[99,3],[],[],"\274\f",[15],[2],[5],"H\b",[],[],[1],[],[7],"\n",[]}}}
11> sets:to_list(Set).
[3,99,12,188,15,2,5,8,72,1,7,10]
|
Note that sets:to_list(sets:from_list(L)) produces an unreliably arranged list.
Using a General Balanced Tree Set (gb_set)
Erlang's standard libraries includes an implementation of Professor Arne Andersson's General Balanced Trees. These structures are more costly than sorting lists for small sets, but this is a much more efficient implementation when working with large sets of data.
The gb_set:from_list function will produce an ordered set of elements (dropping duplicates). The set can then be extracted back to a list for other use:
3> GBSet = gb_sets:from_list(UL).
{12,
{10,
{5,{2,{1,nil,nil},{3,nil,nil}},{8,{7,nil,nil},nil}},
{72,{15,{12,nil,nil},nil},{188,{99,nil,nil},nil}}}}
4> gb_sets:to_list(GBSet).
[1,2,3,5,7,8,10,12,15,72,99,188]
|

Digg It
Del.icio.us
Reddit
Facebook
Stumble Upon
Technorati

