Üye Kayıt Üye Giriş

Dizideki tekrarlanan elemanları silme


Dizideki tekrarlanan elemanları silme

 
// 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);

Bilgisayar Dershanesi Ders Sahibi;
Bilgisayar Dershanesi

Yorumlar

Yorum Yapabilmek İçin Üye Girişi Yapmanız Gerekmektedir.

ETİKETLER