當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Scala Queue distinct()用法及代碼示例


distinct()方法用於從給定隊列中刪除重複的元素。

函數定義: def distinct: Queue[A]

返回類型: It returns a new queue that contains only distinct elements.



範例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)


相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 Scala Queue distinct() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。