skip to Main Content

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

Sample List

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" }

            };

Expected Result

    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" }

        };

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

    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);

                            }
                    }
                }
        
        

3

Answers


  1. This will remove all adjacent entries that are equal.

    for (int i = 0; i < 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 < data.Count; i++)
        data[i].ID = i + 1;
    
    Login or Signup to reply.
  2. 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) => i == 0 || x.Name != data[i - 1].Name)
                .Select((r, index) => 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<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 };
                }
            }
        }
    
    
    }
    
    
    Login or Signup to reply.
  3. Hum, the re-numbering of the ID is a bit of monkey wrench.

    So, this:

            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}"); 
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search