Scala Stack distinct() method with example
Last Updated :
03 Nov, 2019
In Scala Stack class
, the distinct() method is utilized to delete the duplicate elements from the given stack.
Method Definition: def distinct: Stack[A]
Return Type: It returns a new stack with only distinct elements.
Example #1:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( 1 , 3 , 2 , 1 , 2 , 1 , 3 , 2 , 3 , 3 )
println(s 1 )
val result = s 1 .distinct
print( "Stack with distinct elements: " + result)
}
}
|
Output:
Stack(1, 3, 2, 1, 2, 1, 3, 2, 3, 3)
Stack with distinct elements: Stack(1, 3, 2)
Example #2:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( "g" , "e" , "e" , "k" , "s" , "f" , "o" , "r" , "g" , "e" , "e" , "k" , "s" )
println(s 1 )
val result = s 1 .distinct
print( "Stack with distinct elements: " + result)
}
}
|
Output:
Stack(g, e, e, k, s, f, o, r, g, e, e, k, s)
Stack with distinct elements: Stack(g, e, k, s, f, o, r)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...