Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#比较2列表<;字符串>';一行一行_C#_.net - Fatal编程技术网

c#比较2列表<;字符串>';一行一行

c#比较2列表<;字符串>';一行一行,c#,.net,C#,.net,我有两个列表数组。一个叫“朋友”,另一个叫“追随者”。两个数组都包含大量id号。我想逐行比较这两个列表,并创建一个新列表,其中包含两个列表中未出现的项目 以下是我的代码: List<string> notfollowingmelist = new List<string>(); foreach (string friend in friends) { bool isfriend = false;

我有两个列表数组。一个叫“朋友”,另一个叫“追随者”。两个数组都包含大量id号。我想逐行比较这两个列表,并创建一个新列表,其中包含两个列表中未出现的项目

以下是我的代码:

List<string> notfollowingmelist = new List<string>();

        foreach (string friend in friends)
        {
            bool isfriend = false;
            foreach (string follower in followers)
            {
                if (friend == follower)
                {
                    isfriend = true;
                }
                if (isfriend)
                {
                    isfriend = false;

                }
                else
                {
                    notfollowingmelist.Add(friend);


                }
            }

        }

        MessageBox.Show(notfollowingmelist.Count.ToString());
List notfollowermelist=新列表();
foreach(朋友中的字符串朋友)
{
bool isfriend=false;
foreach(跟随器中的字符串跟随器)
{
如果(朋友==跟随者)
{
isfriend=true;
}
如果(是朋友)
{
isfriend=false;
}
其他的
{
notfollowermelist.Add(朋友);
}
}
}
Show(notfollowermelist.Count.ToString());
我这样做是正确的还是有更好的解决方案?

LINQ解决方案 出现在两个列表上的编号:

friends.Intersect(followers);
至少一个列表中出现的所有数字:

friends.Union(followers);
列表中出现的所有数字正好是其中一个:

var intersectResult = friends.Intersect(followers);
var unionResult = friends.Union(followers);

var exactlyOnce = unionResult.Exclude(intersectResult);

这就是您想要的吗?

我将使用类似于快速排序的方法对两个列表进行排序,然后将这两个列表一起逐步浏览,以确定哪些项目是唯一的。排序需要O(nlogn)时间,单步遍历列表需要O(n)时间,总体时间复杂度为O(nlogn)。您当前的实现需要O(n^2)个时间,这要慢一些

下面是一些伪代码:

friends.qsort()
followers.qsort()
disjointList = new List()
int i=0
int j=0
while(i<friends.size() && j<followers.size()){
    if(friends[i] == followers[j]){
        i++
        j++
    }else if(friends[i] < followers[j]){
        disjointList.add(friends[i])
        i++
    }else{
        disjointList.add(followers[j])  // note: omit this line if you only want a list of friends that are not followers
        j++
    }
}
friends.qsort()
followers.qsort()
disjointList=新列表()
int i=0
int j=0

虽然(iLinq是正确的方法,但这里有另一种方法:

List<string> notFollowingMe = friends.Except(followers).ToList();
List notfollowMe=friends.Except(followers.ToList();
IEnumerable notFollowers=friends.Where(x=>!followers.Contains(x));
顺便说一句:您的代码不正确。

(在VB.Net中)逻辑是相同的,应该可以工作

For Each s As String In friends
    If Not (followers.Contains(s)) Then
        notfollowingmelist.Add(s)
    End If
Next
For Each s As String In followers
    If Not (friends.Contains(s)) Then
        notfollowingmelist.Add(s)
    End If
Next

这不是一个好的解决方案。代码不仅要完成它的工作,而且应该是其他开发人员可以阅读的。在没有任何上下文的情况下,试图找出这个函数的功能将比公认的答案花费成倍的时间。在某些情况下,可能需要类似的内容,但这不是其中之一。效率极低的解决方案不推荐!但这是一个很好的演示,演示了如何人为地、无用地使你的生活复杂化。小心,这个解决方案很可能是O(n^2)。@aquinas:Aggree,有人能证实这一点吗?它肯定是
O(n^2)
:你为每个朋友循环一次追随者。请参阅我答案中的“设置操作”方法,以获得更有效的版本。@布兰登,如果两个列表很大,这种方法会快得多,而且速度也是一个问题。生活本身是复杂的,有时你无法简化它。首先,如何循环列表a比在单个列表上执行LINQ函数快得多吗?其次,您的逻辑有问题。您将如何循环遍历追随者列表(根据定义,跟随您的人)我想说Brandon的方法很快,但我不知道如何理解他的解决方案。我的解决方案只是供参考,我可能不太理解这个问题。scott,我的逻辑是fineHi scott,在Brandon的代码中,它没有注释,也不可读,有点:),但逻辑仍然清晰。如果您对两个列表进行了排序,并按顺序同时比较这两个列表,您将能够找到常见的元素。
For Each s As String In friends
    If Not (followers.Contains(s)) Then
        notfollowingmelist.Add(s)
    End If
Next
For Each s As String In followers
    If Not (friends.Contains(s)) Then
        notfollowingmelist.Add(s)
    End If
Next