在C#中移除相邻重复项

huangapple go评论78阅读模式
英文:

Remove adjacent duplicates in List C#

问题

以下是已翻译的内容:

我有一个以下的列表,我只需要删除相邻的重复项。

示例列表
-------------

var data = new List<NewsModel>
{
    new NewsModel { ID = 1, Name = "AAA" },
    new NewsModel { ID = 2, Name = "AAA" },
    new NewsModel { ID = 3, Name = "BBB" },
    new NewsModel { ID = 4, Name = "CCC" },
    new NewsModel { ID = 5, Name = "CCC" },
    new NewsModel { ID = 6, Name = "AAA" },
    new NewsModel { ID = 7, Name = "DDD" },
    new NewsModel { ID = 8, Name = "DDD" },
    new NewsModel { ID = 9, Name = "CCC" }
};

期望结果
----------------

var data = new List<NewsModel>
{
    new NewsModel { ID = 1, Name = "AAA" },
    new NewsModel { ID = 2, Name = "BBB" },
    new NewsModel { ID = 3, Name = "CCC" },
    new NewsModel { ID = 4, Name = "AAA" },
    new NewsModel { ID = 5, Name = "DDD" },
    new NewsModel { ID = 6, Name = "CCC" }
};

我尝试过像下面这样,但它没有达到预期的结果。

foreach (var item in NewsModel.ToList())
{
    if (NewsModel.Count() >= NewsModel.IndexOf(item) + 1)
    {
        _nextItem = NewsModel[NewsModel.IndexOf(item) + 1];

        if (item.Name == _nextItem.Name)
        {
            NewsModel.Remove(item);
        }
    }
}

希望这对你有所帮助。

英文:

I have a below list and i need to remove adjacent duplicate item only.

Sample List

var data = new List&lt;NewsModel&gt;
            {
                new NewsModel { ID = 1, Name = &quot;AAA&quot; },
                new NewsModel { ID = 2, Name = &quot;AAA&quot; },
				new NewsModel { ID = 3, Name = &quot;BBB&quot; },
                new NewsModel { ID = 4, Name = &quot;CCC&quot; },
				new NewsModel { ID = 5, Name = &quot;CCC&quot; },
                new NewsModel { ID = 6, Name = &quot;AAA&quot; },
				new NewsModel { ID = 7, Name = &quot;DDD&quot; },
                new NewsModel { ID = 8, Name = &quot;DDD&quot; },
				new NewsModel { ID = 9, Name = &quot;CCC&quot; }

            };

Expected Result

	var data = new List&lt;NewsModel&gt;
        {
            new NewsModel { ID = 1, Name = &quot;AAA&quot; },
			new NewsModel { ID = 2, Name = &quot;BBB&quot; },
            new NewsModel { ID = 3, Name = &quot;CCC&quot; },
            new NewsModel { ID = 4, Name = &quot;AAA&quot; },
			new NewsModel { ID = 5, Name = &quot;DDD&quot; },
			new NewsModel { ID = 6, Name = &quot;CCC&quot; }

        };

I have tried it like below,But it's not came as expected result..

    foreach (var item in NewsModel.ToList())
                {
                    if (NewsModel.Count() &gt;= NewsModel.IndexOf(item) + 1)
                    {

                            _nextItem = NewsModel[NewsModel.IndexOf(item) + 1];

                            if (item.Name == _nextItem.Name)
                            {

                                NewsModel.Remove(item);

                            }
                    }
                }

答案1

得分: 3

这将删除所有相邻的相同条目。

for (int i = 0; i < data.Count-1; i++)
{
    if (data[i].Name == data[i+1].Name)
    {
        data.RemoveAt(i);
        i--;
    }
}

编辑:

我忽略了你希望在最后更新 ID。在我发送的第一个循环之后再做一个循环,实现这个。

for (int i = 0; i < data.Count; i++)
    data[i].ID = i + 1;
英文:

This will remove all adjacent entries that are equal.

for (int i = 0; i &lt; data.Count-1; i++)
{
    if (data[i].Name == data[i+1].Name)
    {
        data.RemoveAt(i);
        i--;
    }
}

EDIT

I missed that you want the ID's to be updated by the end.
Do another for loop after the first one I sent, that does this

for (int i = 0; i &lt; data.Count; i++)
    data[i].ID = i + 1;

答案2

得分: 1

嗯,ID的重新编号有点复杂。

所以,这个代码会这样:

var data = new List<NewsModel>
{
    new NewsModel { ID = 1, Name = "AAA" },
    new NewsModel { ID = 2, Name = "AAA" },
    new NewsModel { ID = 3, Name = "BBB" },
    new NewsModel { ID = 4, Name = "CCC" },
    new NewsModel { ID = 5, Name = "CCC" },
    new NewsModel { ID = 6, Name = "AAA" },
    new NewsModel { ID = 7, Name = "DDD" },
    new NewsModel { ID = 8, Name = "DDD" },
    new NewsModel { ID = 9, Name = "CCC" }
};

var data2 = new List<NewsModel>();
int iID = 0;
string sPrevious = "";
for (int j = 0; j < data.Count; j++)
{
    NewsModel OneNews = data[j];
    if (sPrevious != OneNews.Name)
    {
        iID++;
        OneNews.ID = iID;
        data2.Add(OneNews);
        sPrevious = OneNews.Name;
    }
}

foreach (NewsModel OneNews in data2)
    Debug.Print($"ID = {OneNews.ID}, Name = {OneNews.Name}");

输出:

ID = 1, Name = AAA
ID = 2, Name = BBB
ID = 3, Name = CCC
ID = 4, Name = AAA
ID = 5, Name = DDD
ID = 6, Name = CCC

如果你要求“原地”从现有列表中删除,或者像上面那样创建一个新列表,这都是一个复杂的选择。

英文:

Hum, the re-numbering of the ID is a bit of monkey wrench.

So, this:

        var data = new List&lt;NewsModel&gt;
        {
            new NewsModel { ID = 1, Name = &quot;AAA&quot; },
            new NewsModel { ID = 2, Name = &quot;AAA&quot; },
            new NewsModel { ID = 3, Name = &quot;BBB&quot; },
            new NewsModel { ID = 4, Name = &quot;CCC&quot; },
            new NewsModel { ID = 5, Name = &quot;CCC&quot; },
            new NewsModel { ID = 6, Name = &quot;AAA&quot; },
            new NewsModel { ID = 7, Name = &quot;DDD&quot; },
            new NewsModel { ID = 8, Name = &quot;DDD&quot; },
            new NewsModel { ID = 9, Name = &quot;CCC&quot; }

        };

        var data2 = new List&lt;NewsModel&gt;();
        int iID = 0;
        string sPrevious = &quot;&quot;;
        for (int j = 0; j &lt; data.Count;j++)
        {
            NewsModel OneNews = data[j];
            if (sPrevious != OneNews.Name)
            {
                iID++;
                OneNews.ID = iID;
                data2.Add(OneNews);
                sPrevious= OneNews.Name;    
            }
        }

        foreach(NewsModel OneNews in data2)
            Debug.Print($&quot;ID = {OneNews.ID}, Name = {OneNews.Name}&quot;); 

Output:

ID = 1, Name = AAA
ID = 2, Name = BBB
ID = 3, Name = CCC
ID = 4, Name = AAA
ID = 5, Name = DDD
ID = 6, Name = CCC

Tough call if your requiring is to "in place" remove from existing list, or as the above does, and creates a new list.

答案3

得分: 0

这是你提供的内容的中文翻译:

Linq

您可以使用Linq来创建没有相邻重复项的列表,并可以在Select中生成带有索引的新ID。

data = data
            .Where((x, i) => i == 0 || x.Name != data[i - 1].Name)
            .Select((r, index) => new { ID = index + 1, Name = r.Name }).ToList();

结果

ID Name
1 AAA
2 BBB
3 CCC
4 AAA
5 DDD
6 CCC

iterator

使用

data = Common.RemoveAdjecentDuplicates(data);

public static class Common
{
    internal static IEnumerable<NewsModel> RemoveAdjecentDuplicates(IEnumerable<NewsModel> newsModel)
    {
        using (var iterator = newsModel.GetEnumerator())
        {
            if (!iterator.MoveNext()) yield break;

            var current = iterator.Current;
            int Index = 1;
            yield return new NewsModel() { Name = current.Name, ID = Index };

            while (iterator.MoveNext())
            {
                if (iterator.Current.Name.Equals(current.Name)) continue;
                current = iterator.Current;
                Index++;
                yield return new NewsModel() { Name = current.Name, ID = Index };
            }
        }
    }
}
英文:

There are several methods. I wrote two methods(linq,iterator)

Linq

You can use Linq to create List without duplicates adjacent and can generate new Id with Index in Select

data = data
            .Where((x, i) =&gt; i == 0 || x.Name != data[i - 1].Name)
            .Select((r, index) =&gt; new { ID = index + 1, Name = r.Name }).ToList();

Result

ID Name
1 AAA
2 BBB
3 CCC
4 AAA
5 DDD
6 CCC

iterator

use

data = Common.RemoveAdjecentDuplicates(data);

Classes

public static class Common
{
    internal static IEnumerable&lt;NewsModel&gt; RemoveAdjecentDuplicates(IEnumerable&lt;NewsModel&gt; newsModel)
    {
        using (var iterator = newsModel.GetEnumerator())
        {
            if (!iterator.MoveNext()) yield break;

            var current = iterator.Current;
            int Index = 1;
            yield return new NewsModel() { Name = current.Name, ID = Index };

            while (iterator.MoveNext())
            {
                if (iterator.Current.Name.Equals(current.Name)) continue;
                current = iterator.Current;
                Index++;
                yield return new NewsModel() { Name = current.Name, ID = Index };
            }
        }
    }


}

huangapple
  • 本文由 发表于 2023年6月9日 02:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定