Removes the duplicates items from array. - CSharp System

CSharp examples for System:Array Operation

Description

Removes the duplicates items from array.

Demo Code


using System.Collections.Generic;

public class Main{
       /// <summary>
       /// Removes the duplicates items from array.
       /// </summary>
       /// <returns>The duplicates.</returns>
       /// <param name="items">The items.</param>
       /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static T[] RemoveDuplicates<T>(this T[] items)
        {//from   ww  w  .  j  a  v a 2  s  . c  om
            List<T> uniqueItems = new List<T>();

            for (int i = 0; i < items.Length; i++)
            {
                if (!uniqueItems.Contains(items[i]))
                {
                    uniqueItems.Add(items[i]);
                }
            }

            return uniqueItems.ToArray();
        }
}

Related Tutorials