📜  Scala List distinct() 方法与示例

📅  最后修改于: 2022-05-13 01:54:20.182000             🧑  作者: Mango

Scala List distinct() 方法与示例

distinct()方法用于从声明的列表中删除重复的元素。

示例 #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)