二つの配列から/ソート項目を比較するための最も効率的な方法は何ですか?

StackOverflow https://stackoverflow.com/questions/1140220

質問

私は、効率的な実装について質問があります。

:私は2つの配列を考えてみましょう
One array is all possible items in a house: Table, Chair, TV, Fireplace, Bed

The other is an array of items in a particular house: Table, TV, Bed

私はまた、2つのリストボックスがあります:

1. listbox for items in the house - the "HAS" list box
2. listbox items not in the house - the "NEEDS" list box

私は、リストボックスと同様に「NEEDS」リストボックスの家にされていない項目を「HAS」で、すでに家の中の項目をリストする必要があります。ループ、この問題を解決するためのスタートになりますが、私は入れ子にする必要がある場合、正確にわからない「それぞれがについて」ネストされたように私には思えます。このような課題を達成するのに最も効率的な方法は何ですか?

役に立ちましたか?

解決

var allItems = (new [] {"Table", "Chair", "TV", "Fireplace", "Bed"});
var hasItems = (new [] {"Table", "Chair"});

var hasList = hasItems.ToList();
var needsList = allItems.Except(hasItems).ToList();

他のヒント

var allList = (new [] {"Table", "Chair", "TV", "Fireplace", "Bed"}).ToList();
var hasList = (new [] {"Table", "Chair"}).ToList();

var hasSet = new HashSet<string>(hasList);
var needsList = allList.Where(i => !hasList.Contains(i)).ToList();

これは〜(少なくとも、大きなO記法の中)最速のソリューションです。

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