Advertisement
Fhernd

EliminacionDuplicados.cs

Mar 27th, 2016
8,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Articulo.Pregunta.P1820
  6. {
  7.     public class EliminacionDuplicados
  8.     {
  9.         public static void Main()
  10.         {
  11.             string[] software = {"Evernote", "Chrome", "TuneIn", "Evernote", "Wolfram Alpha", "TuneIn", "Stack Exchange"};
  12.            
  13.             string[] sinDuplicadosSoftware = EliminarDuplicadosLinq(software);
  14.            
  15.             Console.WriteLine ("\nEliminación duplicados con LINQ:");
  16.             foreach(string sw in sinDuplicadosSoftware)
  17.             {
  18.                 Console.WriteLine (sw);
  19.             }
  20.            
  21.             Console.WriteLine ("\nEliminación duplicados con ciclos foreach:");
  22.             sinDuplicadosSoftware = EliminarDuplicadosIterativo(software);
  23.            
  24.             foreach(string sw in sinDuplicadosSoftware)
  25.             {
  26.                 Console.WriteLine (sw);
  27.             }
  28.            
  29.             Console.WriteLine ();
  30.         }
  31.        
  32.         // Elimina duplicados de un arreglo usando LINQ:
  33.         public static string[] EliminarDuplicadosLinq(string[] software)
  34.         {
  35.             string[] sinDuplicadosSoftware = software.Distinct().ToArray();
  36.            
  37.             return sinDuplicadosSoftware;
  38.         }
  39.        
  40.         // Elimina duplicados de un arreglo usando ciclos:
  41.         public static string[] EliminarDuplicadosIterativo(string[] software)
  42.         {
  43.             List<string> sinDuplicadosSoftware = new List<string>();
  44.            
  45.             foreach(string sw in software)
  46.             {
  47.                 if (!sinDuplicadosSoftware.Contains(sw))
  48.                 {
  49.                     sinDuplicadosSoftware.Add(sw);
  50.                 }
  51.             }
  52.            
  53.             return sinDuplicadosSoftware.ToArray();
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement