Método Queue.Contains() en C#

Este método se utiliza para comprobar si un elemento está en la cola. Este método realiza una búsqueda lineal, por lo tanto, este método es una operación O(n), donde n es Count. Y este método viene bajo el espacio de nombres System.Collections.

Sintaxis:  

public virtual bool Contains(object obj);

Aquí, obj es el objeto a ubicar en la cola. El valor puede ser nulo.

Valor devuelto: la función devuelve True si el elemento existe en la Cola y devuelve False si el elemento no existe en la Cola.

A continuación se dan algunos ejemplos para entender la implementación de una mejor manera:

Ejemplo 1: 

C#

// C# code to illustrate the
// Queue.Contains() Method
using System;
using System.Collections;
 
class GFG {
 
    // Driver code
    public static void Main()
    {
 
        // Creating a Queue
        Queue myQueue = new Queue<int>();
 
        // Inserting the elements into the Queue
        myQueue.Enqueue(5);
        myQueue.Enqueue(10);
        myQueue.Enqueue(15);
        myQueue.Enqueue(20);
        myQueue.Enqueue(25);
 
        // Checking whether the element is
        // present in the Queue or not
        // The function returns True if the
        // element is present in the Queue, else
        // returns False
        Console.WriteLine(myQueue.Contains(7));
    }
}
Producción: 

False

 

Ejemplo 2:

C#

// C# code to illustrate the
// Queue.Contains() Method
using System;
using System.Collections;
 
class GFG {
 
    // Driver code
    public static void Main()
    {
 
        // Creating a Queue of strings
        Queue myQueue = new Queue();
 
        // Inserting the elements into the Queue
        myQueue.Enqueue("Geeks");
        myQueue.Enqueue("Geeks Classes");
        myQueue.Enqueue("Noida");
        myQueue.Enqueue("Data Structures");
        myQueue.Enqueue("GeeksforGeeks");
 
        // Checking whether the element is
        // present in the Queue or not
        // The function returns True if the
        // element is present in the Queue, else
        // returns False
        Console.WriteLine(myQueue.Contains("GeeksforGeeks"));
    }
}
Producción: 

True

 

Referencia: 

Publicación traducida automáticamente

Artículo escrito por Kirti_Mangal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *