📜  Scala Queue distinct() 方法与示例

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

Scala Queue distinct() 方法与示例

distinct()方法用于从给定队列中删除重复元素。

示例 #1:

// Scala program of distinct() 
// method 
  
// Import Queue 
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating queue
        val s1 = Queue(2, 1, 3, 2, 1, 2, 3) 
          
        // Print the queue
        println(s1) 
          
        // Applying distinct() method  
        val result = s1.distinct
          
        // Display output 
        print("Queue with distinct elements: " + result) 
           
    } 
} 
输出:
Queue(2, 1, 3, 2, 1, 2, 3)
Queue with distinct elements: Queue(2, 1, 3)

示例 #2:

// Scala program of distinct() 
// method 
  
// Import Queue 
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating queue
        val s1 = Queue("g", "e", "e", "k", "s", "f", "o", "r", "g", "e", "e", "k", "s") 
          
        // Print the queue
        println(s1) 
          
        // Applying distinct() method  
        val result = s1.distinct
          
        // Display output 
        print("Queue with distinct elements: " + result) 
           
    } 
} 
输出:
Queue(g, e, e, k, s, f, o, r, g, e, e, k, s)
Queue with distinct elements: Queue(g, e, k, s, f, o, r)