質問

誰でも速い方法のためのデ-複製汎用リストのC#?

役に立ちましたか?

解決

ものを使うことを検討すべき HashSet.

らにMSDNリンク:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        HashSet<int> evenNumbers = new HashSet<int>();
        HashSet<int> oddNumbers = new HashSet<int>();

        for (int i = 0; i < 5; i++)
        {
            // Populate numbers with just even numbers.
            evenNumbers.Add(i * 2);

            // Populate oddNumbers with just odd numbers.
            oddNumbers.Add((i * 2) + 1);
        }

        Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);
        DisplaySet(evenNumbers);

        Console.Write("oddNumbers contains {0} elements: ", oddNumbers.Count);
        DisplaySet(oddNumbers);

        // Create a new HashSet populated with even numbers.
        HashSet<int> numbers = new HashSet<int>(evenNumbers);
        Console.WriteLine("numbers UnionWith oddNumbers...");
        numbers.UnionWith(oddNumbers);

        Console.Write("numbers contains {0} elements: ", numbers.Count);
        DisplaySet(numbers);
    }

    private static void DisplaySet(HashSet<int> set)
    {
        Console.Write("{");
        foreach (int i in set)
        {
            Console.Write(" {0}", i);
        }
        Console.WriteLine(" }");
    }
}

/* This example produces output similar to the following:
 * evenNumbers contains 5 elements: { 0 2 4 6 8 }
 * oddNumbers contains 5 elements: { 1 3 5 7 9 }
 * numbers UnionWith oddNumbers...
 * numbers contains 10 elements: { 0 2 4 6 8 1 3 5 7 9 }
 */

他のヒント

います。純3+を使用Linq.

List<T> withDupes = LoadSomeData();
List<T> noDupes = withDupes.Distinct().ToList();

かについて-

var noDupes = list.Distinct().ToList();

ます。純3.5?

単なる初期化パラメータは、HashSetのと同じタイプ:

var noDupes = new HashSet<T>(withDupes);

やしたい場合は、リストを返す:

var noDupsList = new HashSet<T>(withDupes).ToList();

ソートで確認できれば、二つの二次互いに重複塊。

のようなこと:

list.Sort();
Int32 index = list.Count - 1;
while (index > 0)
{
    if (list[index] == list[index - 1])
    {
        if (index < list.Count - 1)
            (list[index], list[list.Count - 1]) = (list[list.Count - 1], list[index]);
        list.RemoveAt(list.Count - 1);
        index--;
    }
    else
        index--;
}

注記:

  • 較からロ、を避けるためのリゾートリストの後に除去
  • この例では、現在、C#の値はタプルの入れ替え、適切なコードが使えない場合は、その
  • 最終結果ではなくソートされ

たところ、うまくいったのです。単に利用

List<Type> liIDs = liIDs.Distinct().ToList<Type>();

交換"タイプ"をご希望のタイプ例int.

私はこのコマンド:

List<Store> myStoreList = Service.GetStoreListbyProvince(provinceId)
                                                 .GroupBy(s => s.City)
                                                 .Select(grp => grp.FirstOrDefault())
                                                 .OrderBy(s => s.City)
                                                 .ToList();

私はこれらの分野のマイリスト:Id、StoreName市PostalCode たいと思ったショート都市で、ドロップダウンが重複した値です。【解決グループによる都市そしてその最初の一覧です。

ってほしいです:)

としてkronozいます。純3.5利用できる Distinct().

ます。純2が真似で

public IEnumerable<T> DedupCollection<T> (IEnumerable<T> input) 
{
    var passedValues = new HashSet<T>();

    // Relatively simple dupe check alg used as example
    foreach(T item in input)
        if(passedValues.Add(item)) // True if item is new
            yield return item;
}

使われることがdedupe他のコレクションの戻り値は、元のです。

通常、もっと早くフィルターを集めても Distinct() このサンプル)のように取り除項目からします。

拡張手法があ道...のようなこと:

public static List<T> Deduplicate<T>(this List<T> listToDeduplicate)
{
    return listToDeduplicate.Distinct().ToList();
}

を呼び出すことは、例えば:

List<int> myFilteredList = unfilteredList.Deduplicate();

JavaいはC#には以下同じ):

list = new ArrayList<T>(new HashSet<T>(list))

また変更のリスト:

List<T> noDupes = new ArrayList<T>(new HashSet<T>(list));
list.clear();
list.addAll(noDupes);

保存、単に置き換えHashSetとLinkedHashSet.

利用Linqの 組合 方法。

注意:このソリューションが必要な知識のLinq、その他には世界最大の国際人権ngoです。

コード

まず、追加の上部のクラスファイル:

using System.Linq;

現在は以下のようなものを使いますの除去を複製することにより、オブジェクト、 obj1:

obj1 = obj1.Union(obj1).ToList();

注意:名前の変更 obj1 の名称のオブジェクトです。

その作品

  1. Euのコマンドリストの各エントリのソースオブジェクト。以来、obj1がともに源泉物を削減できるこobj1の各入力します。

  2. ToList() 新たに追加された仕様一覧です。する必要があることがわかり、Linqのコマンドのように Union 結果を返しますとIEnumerable結果ではなく改変のリストは新一覧です。

さえ気にしなければ、注文ができるた故-パー-メッツの損失の項目へ HashSet, た場合、 い秩序を維持できるようにす:

var unique = new List<T>();
var hs = new HashSet<T>();
foreach (T t in list)
    if (hs.Add(t))
        unique.Add(t);

またはLinq方法:

var hs = new HashSet<T>();
list.All( x =>  hs.Add(x) );

編集:HashSet 方法 O(N) 時間 O(N) スペースを仕分けして世界をリードするユニークなどが示するには以下のようにlassevk その他) O(N*lgN) 時間 O(1) スペースで明確でなくとも一見するとその選別方法は劣るものの私の謝罪の下投票...)

この拡張子を除去する方法隣接する複in-situ.コSort()最初のパ同IComparer.このことより効率的Lasse V.KarlsenのバージョンをRemoveAt繰り返しに伴う変更はなく、複数のブロックメモリ。

public static void RemoveAdjacentDuplicates<T>(this List<T> List, IComparer<T> Comparer)
{
    int NumUnique = 0;
    for (int i = 0; i < List.Count; i++)
        if ((i == 0) || (Comparer.Compare(List[NumUnique - 1], List[i]) != 0))
            List[NumUnique++] = List[i];
    List.RemoveRange(NumUnique, List.Count - NumUnique);
}

としてのヘルパー方式(Linq):

public static List<T> Distinct<T>(this List<T> list)
{
    return (new HashSet<T>(list)).ToList();
}

設置の MoreLINQ パッケージによNuget、簡単なオブジェクト-コントローラーによる物件の

IEnumerable<Catalogue> distinctCatalogues = catalogues.DistinctBy(c => c.CatalogueCode); 

そこに独特の要素が重複することなく要素)に変換するリストしてもらうことを目的として

List<type> myNoneDuplicateValue = listValueWithDuplicate.Distinct().ToList();

するのが簡単で確実に重複しないリストに追加されます。

if(items.IndexOf(new_item) < 0) 
    items.add(new_item)

別の言い方です。純2.0

    static void Main(string[] args)
    {
        List<string> alpha = new List<string>();

        for(char a = 'a'; a <= 'd'; a++)
        {
            alpha.Add(a.ToString());
            alpha.Add(a.ToString());
        }

        Console.WriteLine("Data :");
        alpha.ForEach(delegate(string t) { Console.WriteLine(t); });

        alpha.ForEach(delegate (string v)
                          {
                              if (alpha.FindAll(delegate(string t) { return t == v; }).Count > 1)
                                  alpha.Remove(v);
                          });

        Console.WriteLine("Unique Result :");
        alpha.ForEach(delegate(string t) { Console.WriteLine(t);});
        Console.ReadKey();
    }

さまざまですが、解決の複製、リストの下で:

List<Container> containerList = LoadContainer();//Assume it has duplicates
List<Container> filteredList = new  List<Container>();
foreach (var container in containerList)
{ 
  Container duplicateContainer = containerList.Find(delegate(Container checkContainer)
  { return (checkContainer.UniqueId == container.UniqueId); });
   //Assume 'UniqueId' is the property of the Container class on which u r making a search

    if(!containerList.Contains(duplicateContainer) //Add object when not found in the new class object
      {
        filteredList.Add(container);
       }
  }

乾杯 Ravi Ganesan

こちらは簡単に解決を必要としない他のハード読みLINQ又は事前選別をクリックします。

   private static void CheckForDuplicateItems(List<string> items)
    {
        if (items == null ||
            items.Count == 0)
            return;

        for (int outerIndex = 0; outerIndex < items.Count; outerIndex++)
        {
            for (int innerIndex = 0; innerIndex < items.Count; innerIndex++)
            {
                if (innerIndex == outerIndex) continue;
                if (items[outerIndex].Equals(items[innerIndex]))
                {
                    // Duplicate Found
                }
            }
        }
    }

David J.の答えは良い方法も必要としないための余分な物を、選別などで改善することができるのに対し

for (int innerIndex = items.Count - 1; innerIndex > outerIndex ; innerIndex--)

その外側のループが上下全体のリストでも内側のループが底"まで、外側の位置ループは".

外側のループであることを確全体のリストが処理され、内側のループが、実際の重複を、できるだことのない部分の外側のループな加工ます。

はだいたいボトムアップの内側のループに内側のループを開始outerIndex+1.

利用できる組合

obj2 = obj1.Union(obj1).ToList();

場合は授業を牽引 ProductCustomer また重複した項目からリスト

public class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }

}

public class Customer
{
    public int Id { get; set; }
    public string CustomerName { get; set; }

}

を定義する必要があ汎用クラスには以下のフォーム

public class ItemEqualityComparer<T> : IEqualityComparer<T> where T : class
{
    private readonly PropertyInfo _propertyInfo;

    public ItemEqualityComparer(string keyItem)
    {
        _propertyInfo = typeof(T).GetProperty(keyItem, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
    }

    public bool Equals(T x, T y)
    {
        var xValue = _propertyInfo?.GetValue(x, null);
        var yValue = _propertyInfo?.GetValue(y, null);
        return xValue != null && yValue != null && xValue.Equals(yValue);
    }

    public int GetHashCode(T obj)
    {
        var propertyValue = _propertyInfo.GetValue(obj, null);
        return propertyValue == null ? 0 : propertyValue.GetHashCode();
    }
}

そして、削除ができます重複した項目に一覧です。

var products = new List<Product>
            {
                new Product{ProductName = "product 1" ,Id = 1,},
                new Product{ProductName = "product 2" ,Id = 2,},
                new Product{ProductName = "product 2" ,Id = 4,},
                new Product{ProductName = "product 2" ,Id = 4,},
            };
var productList = products.Distinct(new ItemEqualityComparer<Product>(nameof(Product.Id))).ToList();

var customers = new List<Customer>
            {
                new Customer{CustomerName = "Customer 1" ,Id = 5,},
                new Customer{CustomerName = "Customer 2" ,Id = 5,},
                new Customer{CustomerName = "Customer 2" ,Id = 5,},
                new Customer{CustomerName = "Customer 2" ,Id = 5,},
            };
var customerList = customers.Distinct(new ItemEqualityComparer<Customer>(nameof(Customer.Id))).ToList();

このコードを削除重複した項目の Id したい場合は削除重複した項目によるその他の物件を変更することができ nameof(YourClass.DuplicateProperty)nameof(Customer.CustomerName) を削除します重複した項目の CustomerName 物件です。

  public static void RemoveDuplicates<T>(IList<T> list )
  {
     if (list == null)
     {
        return;
     }
     int i = 1;
     while(i<list.Count)
     {
        int j = 0;
        bool remove = false;
        while (j < i && !remove)
        {
           if (list[i].Equals(list[j]))
           {
              remove = true;
           }
           j++;
        }
        if (remove)
        {
           list.RemoveAt(i);
        }
        else
        {
           i++;
        }
     }  
  }

簡単に直感的に実施

public static List<PointF> RemoveDuplicates(List<PointF> listPoints)
{
    List<PointF> result = new List<PointF>();

    for (int i = 0; i < listPoints.Count; i++)
    {
        if (!result.Contains(listPoints[i]))
            result.Add(listPoints[i]);
        }

        return result;
    }

すべての回答リストのコピー、または新規リスト、または利用が遅い機能、またはかりにも遅い。

私の理解では、この 最速で最も安い方法 いものの背景には、非常に経験豊富なプログラマの専門がリアルタイムで物理学の最適化。

// Duplicates will be noticed after a sort O(nLogn)
list.Sort();

// Store the current and last items. Current item declaration is not really needed, and probably optimized by the compiler, but in case it's not...
int lastItem = -1;
int currItem = -1;

int size = list.Count;

// Store the index pointing to the last item we want to keep in the list
int last = size - 1;

// Travel the items from last to first O(n)
for (int i = last; i >= 0; --i)
{
    currItem = list[i];

    // If this item was the same as the previous one, we don't want it
    if (currItem == lastItem)
    {
        // Overwrite last in current place. It is a swap but we don't need the last
       list[i] = list[last];

        // Reduce the last index, we don't want that one anymore
        last--;
    }

    // A new item, we store it and continue
    else
        lastItem = currItem;
}

// We now have an unsorted list with the duplicates at the end.

// Remove the last items just once
list.RemoveRange(last + 1, size - last - 1);

// Sort again O(n logn)
list.Sort();

最終コストは:

nlogn+n+nlogn=n+2nlogn= O(nlogn) ですが、ここでは素敵です。

注RemoveRange: この設定をカウントのリストおよび使用は避け、削除funcionsわかんないの速度がこの操作ことができるようにな嬉しいです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top