C# Dizideki tekrarlanan elemanları silme

Konu sahibi son olarak 3311 gün önce görüldü
Array içindeki tekrarlanan elemanları .Net 1.1'de bulunan System.Array sınıfı ile temizler

Kod:
/ Fonksiyon Kodları
public static Array DeleteDuplicates(Array arr)
{
   // this procedure works only with vectors
   if (arr.Rank != 1 )
      throw new ArgumentException("Multiple-dimension arrays are not supported");

   // we use a hashtable to track duplicates
   // make the hash table large enough to avoid memory re-allocations
   Hashtable ht = new Hashtable(arr.Length * 2);
   // we will store unique elements in this ArrayList
   ArrayList elements = new ArrayList();

   foreach (object Value in arr)
   {
      if ( !ht.Contains(Value) )
      {
         // we've found a non duplicate
         elements.Add(Value);
         // remember it for later
         ht.Add(Value, null);
      }
   }
   // return an array of same type as the original array
   return elements.ToArray(arr.GetType().GetElementType());
}

// Örnek Kullanım:
int[] numbers = new int[] {1, 3, 5, 2, 3, 1, 4};
int[] result = (int[]) DropDuplicates(numbers);
foreach (int num in result)
   Console.WriteLine(num);
 
Geri