加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 算法 – 如何有效地删除列表中的重复元素?

发布时间:2020-12-14 16:34:04 所属栏目:Java 来源:网络整理
导读:有一个列表L.它包含任意类型的元素. 如何有效地删除此列表中的所有重复元素?订单必须保留 只需要一个算法,所以不允许导入任何外部库. 相关问题 In Python,what is the fastest algorithm for removing duplicates from a list so that all elements are uni
有一个列表L.它包含任意类型的元素.
如何有效地删除此列表中的所有重复元素?订单必须保留

只需要一个算法,所以不允许导入任何外部库.

相关问题

> In Python,what is the fastest algorithm for removing duplicates from a list so that all elements are unique while preserving order?
> How do you remove duplicates from a list in Python whilst preserving order?
> Removing duplicates from list of lists in Python
> How do you remove duplicates from a list in Python?

解决方法

假设订单事宜:

>创建一个空集S和一个空列表M.
>一次扫描列表L一个元素.
>如果元素在集合S中,请跳过它.
>否则,将其添加到M和S.
>对于L中的所有元素重复
>返回M.

在Python中:

>>> L = [2,1,4,3,5,2,6,5]
>>> S = set()
>>> M = []
>>> for e in L:
...     if e in S:
...         continue
...     S.add(e)
...     M.append(e)
... 
>>> M
[2,6]

如果订单不重要:

M = list(set(L))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读