Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created November 6, 2017 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/30beb51329ec94e1e8f07a5cd77cf824 to your computer and use it in GitHub Desktop.
Save Fhernd/30beb51329ec94e1e8f07a5cd77cf824 to your computer and use it in GitHub Desktop.
Ejemplo de uso de la clase HashSet<T>.
using System;
using System.Collections.Generic;
namespace cap07.colecciones
{
public class UnionConjuntos
{
public static void Main()
{
HashSet<int> pares = new HashSet<int>();
HashSet<int> impares = new HashSet<int>();
for(int i = 1; i <= 10; ++i)
{
if(i % 2 == 0 ){
pares.Add(i);
} else {
impares.Add(i);
}
}
Console.WriteLine("Contenido conjunto números pares:");
MostrarConjunto(pares);
Console.WriteLine("\nContenido conjunto números impares:");
MostrarConjunto(impares);
HashSet<int> numerosEnteros = new HashSet<int>(pares);
numerosEnteros.UnionWith(impares);
Console.WriteLine("\nConjunto resultante de la unión de pares e impares:");
MostrarConjunto(numerosEnteros);
}
public static void MostrarConjunto(HashSet<int> conjunto)
{
foreach(int numero in conjunto)
{
Console.Write("{0} ", numero);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment