Module sets has two functions that can be composed and do the job in an efficient way: sets:from_list/1 returns a set with all the elements of a list (with no duplicated elements from definition) and sets:to_list/1 returns a list with the elements of a set. Here is an example of use:
6条答案
按热度按时间llmtgqce1#
You could use
sets
, for example:This returns
[1,2,3,4,5]
, no more duplicates, but most likely not sorted.Another possibility without the usage of
sets
would be:In this case it returns
[1,2,3,4,5]
, no more duplicates and sorted.wz3gfoph2#
对于那些希望保持列表顺序的人:
5fjcxozz3#
一个可能的解决方案将
Preserve the order of the elements
帮助你学习如何操作列表,将涉及两个函数:为了测试,
qeeaahzv4#
一开始我会做这样的事情来保持秩序,尽管这是不推荐的。记住
AddedStuff ++ Accumulator
是可以的,但Accumulator ++ AddedStuff
真的很糟糕。如果要保持顺序,此解决方案的效率要高得多:
ulydmbyx5#
我认为最好的选择是使用
lists:usort()
但是,如果您不想使用BIF,而希望对列表进行排序,我建议使用快速排序,在此实现中,您将得到没有重复值的排序列表。
2nbm6dog6#
Module
sets
has two functions that can be composed and do the job in an efficient way:sets:from_list/1
returns a set with all the elements of a list (with no duplicated elements from definition) andsets:to_list/1
returns a list with the elements of a set. Here is an example of use:We could define the function as