📜  Scala Stack distinct() 方法与示例

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

Scala Stack distinct() 方法与示例

在 Scala Stack class中, distinct()方法用于从给定堆栈中删除重复元素。

示例 #1:

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

示例 #2:

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