Open In App

Scala Queue distinct() method with example

Last Updated : 03 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The distinct() method is utilized to remove the duplicate elements from the given queue.

Method Definition: def distinct: Queue[A]

Return Type: It returns a new queue that contains only distinct elements.

Example #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) 
           
    


Output:

Queue(2, 1, 3, 2, 1, 2, 3)
Queue with distinct elements: Queue(2, 1, 3)

Example #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) 
           
    


Output:

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)


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads