Scala Queue distinct() method with example
Last Updated :
03 Nov, 2019
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:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Queue( 2 , 1 , 3 , 2 , 1 , 2 , 3 )
println(s 1 )
val result = s 1 .distinct
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:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Queue( "g" , "e" , "e" , "k" , "s" , "f" , "o" , "r" , "g" , "e" , "e" , "k" , "s" )
println(s 1 )
val result = s 1 .distinct
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)
Share your thoughts in the comments
Please Login to comment...