Scala List distinct() 方法与示例
distinct()方法用于从声明的列表中删除重复的元素。
Method Definition: def distinct: List[A]
Return Type: It returns a new list of elements without any duplicates.
示例 #1:
// Scala program of distinct()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(1, 1, 3, 3, 5, 2)
// Applying distinct method
val res = m1.distinct
// Displays output
println(res)
}
}
输出:
List(1, 3, 5, 2)
示例 #2:
// Scala program of distinct()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(1, 1, 3, 3, 3, 5, 4, 5, 2)
// Applying distinct method
val res = m1.distinct
// Displays output
println(res)
}
}
输出:
List(1, 3, 5, 4, 2)