Created
March 27, 2016 15:21
-
-
Save Fhernd/8d640b431e4212845fe8 to your computer and use it in GitHub Desktop.
Demostración de la eliminación de duplicados de datos en un arreglo con C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Articulo.Pregunta.P1820 | |
{ | |
public class EliminacionDuplicados | |
{ | |
public static void Main() | |
{ | |
string[] software = {"Evernote", "Chrome", "TuneIn", "Evernote", "Wolfram Alpha", "TuneIn", "Stack Exchange"}; | |
string[] sinDuplicadosSoftware = EliminarDuplicadosLinq(software); | |
Console.WriteLine ("\nEliminación duplicados con LINQ:"); | |
foreach(string sw in sinDuplicadosSoftware) | |
{ | |
Console.WriteLine (sw); | |
} | |
Console.WriteLine ("\nEliminación duplicados con ciclos foreach:"); | |
sinDuplicadosSoftware = EliminarDuplicadosIterativo(software); | |
foreach(string sw in sinDuplicadosSoftware) | |
{ | |
Console.WriteLine (sw); | |
} | |
Console.WriteLine (); | |
} | |
// Elimina duplicados de un arreglo usando LINQ: | |
public static string[] EliminarDuplicadosLinq(string[] software) | |
{ | |
string[] sinDuplicadosSoftware = software.Distinct().ToArray(); | |
return sinDuplicadosSoftware; | |
} | |
// Elimina duplicados de un arreglo usando ciclos: | |
public static string[] EliminarDuplicadosIterativo(string[] software) | |
{ | |
List<string> sinDuplicadosSoftware = new List<string>(); | |
foreach(string sw in software) | |
{ | |
if (!sinDuplicadosSoftware.Contains(sw)) | |
{ | |
sinDuplicadosSoftware.Add(sw); | |
} | |
} | |
return sinDuplicadosSoftware.ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment