Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<MyType> list1;
- List<MyType> list2;
- Enumerable.SequenceEqual(list1.OrderBy(t => t), list2.OrderBy(t => t))
- public static bool ScrambledEquals<T>(IEnumerable<T> list1, IEnumerable<T> list2) {
- var cnt = new Dictionary<T, int>();
- foreach (T s in list1) {
- if (cnt.ContainsKey(s)) {
- cnt[s]++;
- } else {
- cnt.Add(s, 1);
- }
- }
- foreach (T s in list2) {
- if (cnt.ContainsKey(s)) {
- cnt[s]--;
- } else {
- return false;
- }
- }
- return cnt.Values.All(c => c == 0);
- }
- public static bool ScrambledEquals<T>(IEnumerable<T> list1, IEnumerable<T> list2, IEqualityComparer<T> comparer) {
- var cnt = new Dictionary<T, int>(comparer);
- ...
- // lists should have same count of items, and set difference must be empty
- var areEquivalent = (list1.Count == list2.Count) && !list1.Except(list2).Any();
- // check that [(A-B) Union (B-A)] is empty
- var areEquivalent = !list1.Except(list2).Union( list2.Except(list1) ).Any();
- var a = new[] {1, 2, 3, 4, 4, 3, 1, 1, 2};
- var b = new[] { 4, 3, 2, 3, 1, 1, 1, 4, 2 };
- // result below should be true, since the two sets are equivalent...
- var areEquivalent = (a.Count() == b.Count()) && !a.Except(b).Any();
- var set1 = new HashSet<MyType>(list1);
- var set2 = new HashSet<MyType>(list2);
- return set1.SetEquals(set2);
- list1.All(item => list2.Contains(item)) &&
- list2.All(item => list1.Contains(item));
- list1.All(item => list2.Contains(item)) &&
- list1.Distinct().Count() == list1.Count &&
- list1.Count == list2.Count
- static bool ArePermutations<T>(IList<T> list1, IList<T> list2)
- {
- if(list1.Count != list2.Count)
- return false;
- var l1 = list1.ToLookup(t => t);
- var l2 = list2.ToLookup(t => t);
- return l1.Count == l2.Count
- && l1.All(group => l2.Contains(group.Key) && l2[group.Key].Count() == group.Count());
- }
- // veriables been used
- List<T> diffList = new List<T>();
- List<T> gotResultList = new List<T>();
- // compare First field within my MyList
- gotResultList = MyList1.Where(a => !MyList2.Any(a1 => a1.MyListTField1 == a.MyListTField1)).ToList().Except(gotResultList.Where(a => !MyList2.Any(a1 => a1.MyListTField1 == a.MyListTField1))).ToList();
- // Generate result list
- diffList.AddRange(gotResultList);
- // compare Second field within my MyList
- gotResultList = MyList1.Where(a => !MyList2.Any(a1 => a1.MyListTField2 == a.MyListTField2)).ToList().Except(gotResultList.Where(a => !MyList2.Any(a1 => a1.MyListTField2 == a.MyListTField2))).ToList();
- // Generate result list
- diffList.AddRange(gotResultList);
- MessageBox.Show(diffList.Count.ToString);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement