在两个数组中找到“不相同”的元素

时间:2021-11-23 14:04:56

I have two integer lists (List<int>). They contain the same elements, but List 1 contains elements that are not in the List 2.

我有两个整数列表(List )。它们包含相同的元素,但列表1包含不在列表2中的元素。

How to find which elements of the List 1 ARE NOT in the List 2.

如何查找列表1中哪些元素不在列表2中。

Thanks :)

谢谢 :)

PS. lang is c#

PS。郎是c#

6 个解决方案

#1


3  

new HashSet<int>(l1).ExceptWith(l2);

#2


18  

You can use IEnumerable.Except:

您可以使用IEnumerable.Except:

list1.Except(list2);

#3


1  

A very easy solution:

一个非常简单的解决方

HashSet<int> theSet1 = new HashSet<int>(List1);
theSet1.ExceptWith(List2);

#4


0  

For simplicity you can use the Contains method and check for one list not containing an element of the other:

为简单起见,您可以使用Contains方法并检查一个不包含另一个元素的列表:

for (int i = 0; i < list2.Count; ++i)
{
    if (!list1.Contains(list2[i]) //current element is not in list 1
        //some code
}

#5


0  

If your solution is that firs list contain second and to you hunting onli records added after first list , Maybe this is going to be useful

如果你的解决方案是第一个列表包含第二个和你在第一个列表后添加的onli记录,也许这将是有用的

public static int DokleSuIsti(IList<string> prevzemNow, IList<string> prevzemOld)
        {
            int dobroja = 0;
            int kolikohinaje;
            if (prevzemOld.Count() < prevzemNow.Count())
            {
                kolikohinaje = prevzemOld.Count();
            }
            else
            {
                kolikohinaje = prevzemNow.Count();
            }



            for (int i = 0; i < kolikohinaje; i++)
            {
                if (!Object.Equals(prevzemNow[i], prevzemOld[i]))
                {
                    dobroja = i;
                    return dobroja;
                }
                dobroja = i;
            }
            return dobroja;
        }

After that you can use that int as starting point for walk trough your Ilist

之后,您可以使用该int作为您的Ilist步行的起点

#6


-1  

If they are not sorted or something, you are going to have a hard time.

如果他们没有排序或什么,你将会很难。

Either O(N^2) algorithm (a simple, stupid loop) or additional data structures, tell me which do you prefer.

O(N ^ 2)算法(一个简单的,愚蠢的循环)或其他数据结构,告诉我你更喜欢哪个。

Or, you can of course alter the source data by sorting, which I suppose is not an option.

或者,您当然可以通过排序来更改源数据,我认为这不是一个选项。

#1


3  

new HashSet<int>(l1).ExceptWith(l2);

#2


18  

You can use IEnumerable.Except:

您可以使用IEnumerable.Except:

list1.Except(list2);

#3


1  

A very easy solution:

一个非常简单的解决方

HashSet<int> theSet1 = new HashSet<int>(List1);
theSet1.ExceptWith(List2);

#4


0  

For simplicity you can use the Contains method and check for one list not containing an element of the other:

为简单起见,您可以使用Contains方法并检查一个不包含另一个元素的列表:

for (int i = 0; i < list2.Count; ++i)
{
    if (!list1.Contains(list2[i]) //current element is not in list 1
        //some code
}

#5


0  

If your solution is that firs list contain second and to you hunting onli records added after first list , Maybe this is going to be useful

如果你的解决方案是第一个列表包含第二个和你在第一个列表后添加的onli记录,也许这将是有用的

public static int DokleSuIsti(IList<string> prevzemNow, IList<string> prevzemOld)
        {
            int dobroja = 0;
            int kolikohinaje;
            if (prevzemOld.Count() < prevzemNow.Count())
            {
                kolikohinaje = prevzemOld.Count();
            }
            else
            {
                kolikohinaje = prevzemNow.Count();
            }



            for (int i = 0; i < kolikohinaje; i++)
            {
                if (!Object.Equals(prevzemNow[i], prevzemOld[i]))
                {
                    dobroja = i;
                    return dobroja;
                }
                dobroja = i;
            }
            return dobroja;
        }

After that you can use that int as starting point for walk trough your Ilist

之后,您可以使用该int作为您的Ilist步行的起点

#6


-1  

If they are not sorted or something, you are going to have a hard time.

如果他们没有排序或什么,你将会很难。

Either O(N^2) algorithm (a simple, stupid loop) or additional data structures, tell me which do you prefer.

O(N ^ 2)算法(一个简单的,愚蠢的循环)或其他数据结构,告诉我你更喜欢哪个。

Or, you can of course alter the source data by sorting, which I suppose is not an option.

或者,您当然可以通过排序来更改源数据,我认为这不是一个选项。