scala 去除重复元素

List in Scala is a collection that stores data in the form of a liked-list. The list is an immutable data structure but may contain duplicate elements. And in real life implementation duplicate elements increase the runtime of the program which is not good. We need to keep a check of duplicate elements are remove them from the list.

Scala中的List是一个集合,以喜欢列表的形式存储数据。 该列表是不可变的数据结构,但可能包含重复的元素。 在现实生活中,重复元素会增加程序的运行时间,这是不好的。 我们需要检查重复元素是否从列表中删除。

So, here we are with the Scala program to remove duplicates from list which can be helpful while working with lists.

因此,在这里,我们使用Scala程序从列表删除重复项,这在使用列表时可能会有所帮助。

There are more than one method that can be used to remove duplicates,

有多种方法可以用来删除重复项,

  1. Using distinct method

    使用独特的方法

  2. Converting list into set and then back to list

    将列表转换为集合,然后返回列表

1)使用独特的方法从列表中删除重复项 (1) Remove duplicates from list using distinct method)

The distinct method is used to extract all distinct elements from a list by eliminating all duplicate value from it.

通过从列表中消除所有重复值,使用distinct方法从列表中提取所有独特元素。

Syntax:

句法:

listname.distinct

Program:

程序:

object myObject {
def main(args:Array[String]) {val list = List(23, 44, 97, 12, 23, 12 , 56, 25, 76)
println("The list is : " + list)
val uniqueList = list.distinct
println("The list after removing duplicates is: " + uniqueList)
}
}

Output:

输出:

The list is : List(23, 44, 97, 12, 23, 12, 56, 25, 76)
The list after removing duplicates is: List(23, 44, 97, 12, 56, 25, 76)

2)通过将列表转换为集合,然后返回列表,从列表中删除重复项 (2) Remove duplicates from list by converting list into set and then back to list)

One way to remove duplicate elements from a list is by converting the list to another sequence which does not accept duplicates and then convert it back to list.

从列表中删除重复元素的一种方法是将列表转换为不接受重复的另一个序列,然后再将其转换回列表。

Syntax:

句法:

//Converting list to set:
listName.toSet
//Converting set to list:
setName.toList

Program:

程序:

object myObject{
def main(args:Array[String]) {val list = List(23, 44, 97, 12, 23, 12 , 56, 25, 76)
println("The list is : " + list)
val seq = list.toSet
val uniqueList = seq.toList
println("The list after removing duplicates is: " + uniqueList)
}
}

Output:

输出:

The list is : List(23, 44, 97, 12, 23, 12, 56, 25, 76)
The list after removing duplicates is: List(56, 25, 97, 44, 12, 76, 23)

翻译自: https://www.includehelp.com/scala/remove-duplicates-from-list.aspx

scala 去除重复元素

scala 去除重复元素_Scala程序从列表中删除重复项相关推荐

  1. Python | 程序从列表中删除重复的元素

    Example: 例: Input: list1: [10, 20, 10, 20, 30, 40, 30, 50] Output: List after removing duplicate ele ...

  2. 1.统计所输入字符串中单词的个数。2.删除一个list里面重复元素。3.将列表中的偶数变成它的平方,奇数保持不变。4.输入字符串,将其每个字符的ASCII码形成列表并输出.5.猜单词游戏

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 题目: 1.统计所输入字符串中单词的个数,单词之间用空格分隔. 代码: print("请输入字符(以空格隔开):" ...

  3. python二维数组去重复_python 去除二维数组/二维列表中的重复行方法

    python 去除二维数组/二维列表中的重复行方法 之前提到去除一维数组中的重复元素用unique()函数,如果要去除二维数组中的重复行该怎么操作呢? import numpy as np arr = ...

  4. 如何在保留订单的同时从列表中删除重复项?

    是否有内置的程序在保留顺序的同时从Python列表中删除重复项? 我知道我可以使用集合来删除重复项,但这会破坏原始顺序. 我也知道我可以这样滚动自己: def uniq(input):output = ...

  5. 从Dart列表中删除重复项的2种方法

    本文向您展示了从 Flutter 中的列表中删除重复项的 2 种方法.第一个适用于原始数据类型列表.第二个稍微复杂一些,但适用于map****列表或对象列表. 转换为 Set 然后反转为 List 这 ...

  6. C语言从未排序的链接列表中删除重复项的算法(附完整源码)

    C语言从未排序的链接列表中删除重复项的算法 C语言从未排序的链接列表中删除重复项的算法完整源码(定义,实现,main函数测试) C语言从未排序的链接列表中删除重复项的算法完整源码(定义,实现,main ...

  7. python从后面删除重复项_如何从Python列表中删除重复项

    如何从Python列表中删除重复项 了解如何从Python中的List中删除重复项技巧. 实例 从列表中删除任何重复项: mylist = ["a", "b", ...

  8. python去掉字典重复项_从字典列表中删除重复项python

    我正在尝试从下面的列表中删除重复项distinct_cur = [{'rtc': 0, 'vf': 0, 'mtc': 0, 'doc': 'good job', 'foc': 195, 'st': ...

  9. Python | 程序从列表中删除范围内的所有元素

    Given a list and we have to remove elements in a range from the list in Python. 给定一个列表,我们必须从Python中的 ...

最新文章

  1. 完美解决Invalid layout of java.lang.String at value问题的方法
  2. 不是python对文件的读操作方法的是-一文读懂Python对文件的各种操作方式-阿里云开发者社区...
  3. 椭圆曲线密码学导论pdf_椭圆曲线密码学
  4. 找到符合条件的索引_高频面试题:MySQL联合索引的最左前缀匹配原则
  5. 《JavaScript权威指南》读书笔记二
  6. Servlet 数据库访问
  7. win10一直正在检查更新_教你解决“Win10系统更新失败后循环重启安装”的解决方法...
  8. MyCat全局序列之本地文件方式
  9. Vdbench工具文件系统测试记录
  10. java格式化日期24小时_Java如何格式化24小时格式的时间?
  11. 微信小程序 人脸识别登陆模块
  12. 个人收藏机器学习教程
  13. 超好用的手机开源自动化测试工具分享
  14. 准确生成电信、联通、移动IP地址段
  15. OpenGL Android课程六:介绍纹理过滤
  16. 磨金石教育插画干货分享|日本插画为什么独树一帜,那么受欢迎
  17. 第4-8课:方块消除游戏
  18. 2019年互联网公司月饼颜值大比拼!
  19. 世界主要国家人工智能战略简析
  20. ABBYY OCR技术教电脑阅读缅甸语(下)

热门文章

  1. 儿童吹泡泡水简单配方_儿童吹泡泡玩具水怎么制作
  2. CPR认证-建材CE认证-305/2011/EU
  3. Spring Cloud 学习笔记(一) 之服务治理模块Spring Cloud Eureka 搭建注册中心
  4. CentOS5、6的启动流程
  5. 《Linus Torvalds自传》摘录
  6. 分类算法之贝叶斯(Bayes)分类器
  7. 在Access中执行SQL语句
  8. mimakatz用法_两步完成利用procdump64+mimikatz获取win用户密码
  9. python运算符_Python运算符总结
  10. count(1),count(*),count(主键) 性能对比及辟谣