두 배열에서 항목을 비교/정렬하는 가장 효율적인 방법은 무엇입니까?
-
16-09-2019 - |
문제
효율적인 구현에 대한 질문이 있습니다. 두 개의 배열이 있다고 가정 해 봅시다.
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
두 개의 목록 상자도 있습니다.
1. listbox for items in the house - the "HAS" list box 2. listbox items not in the house - the "NEEDS" list box
"Hass"목록 상자에 이미 집에있는 품목과 "필요"목록 상자에있는 집에없는 품목을 나열해야합니다. "각각의"루프에 대해 중첩 된 것 같습니다.이 문제는이 문제를 해결하기위한 시작일 것입니다. 이와 같은 작업을 수행하는 가장 효율적인 방법은 무엇입니까?
해결책
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 표기법).
제휴하지 않습니다 StackOverflow