Share via


delet the common iteams in list in C#

Question

Wednesday, June 15, 2011 11:43 AM

hi all

i have two list , i wann to search the common iteams in that list and i want to remove it ,

how can i do it ?

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

List<string> l2 = new List<String>();

 

how can i delet the common iteams in the litst l1 and list l2 ?? any one know ?

 

regards

aniruddha

All replies (6)

Wednesday, June 15, 2011 3:02 PM ✅Answered

This is one of the results:

 

      List<string> list1 = new List<string>();
      List<string> list2 = new List<string>();
      list1.Add("item1");
      list1.Add("item2");
      list1.Add("item3");
      list1.Add("item4");
      list2.Add("item1");
      list2.Add("item3");
      list2.Add("item5");


      //looping through both lists and checking for equality
      //if there is an item equal in both lists, they are both added to their stirng arrays:
      string[] items1 = new string[list1.Count];
      string[] items2 = new string[list1.Count];

      for (int i = 0; i < list1.Count; i++)
      {
        for (int j = 0; j < list2.Count; j++)
        {
          if (list1[i] == list2[j])
          {
            items1[i] = list1[i];
            items2[j] = list2[j];
          }
        }
      }

      //delete from lists:
      for (int i = 0; i < items1.Length; i++)
        if (!String.IsNullOrEmpty(items1[i]))
          list1.Remove(items1[i]);
      for (int i = 0; i < items2.Length; i++)
        if (!String.IsNullOrEmpty(items2[i]))
          list2.Remove(items2[i]);

      //showing the result:
      StringBuilder sb = new StringBuilder();
      sb.AppendLine("From list 1:");
      foreach (string str in list1)
        sb.AppendLine(str);
      sb.AppendLine("From list 2:");
      foreach (string str in list2)
        sb.AppendLine(str);
      Console.WriteLine(sb.ToString());
      Console.ReadLine();

Mitja


Wednesday, June 15, 2011 4:43 PM ✅Answered | 1 vote

Had no time to craft a sample till now, e.g.

 

namespace CS
{
 using System.Collections.Generic;
 using System.Linq;
 using Microsoft.VisualStudio.TestTools.UnitTesting;

 [TestClass]
 public class ListRemoval
 {
  public TestContext TestContext { get; set; }

  [TestMethod]
  public void TestMethod1()
  {
   List<string> l1 = new List<string>() { "1", "2", "3" };
   List<string> l2 = new List<string>() { "5", "4", "3" };
   List<string> remove = l1.Intersect(l2).ToList();
   Assert.IsTrue(remove.Count<string>() == 1);
   l1.RemoveAll(s => remove.IndexOf(s) >= 0);
   l2.RemoveAll(s => remove.IndexOf(s) >= 0);
   Assert.IsTrue(l1.Count<string>() == 2);
   Assert.IsTrue(l2.Count<string>() == 2);
  }
 }
}

 

 

Microsoft MVP Office Access
https://mvp.support.microsoft.com/profile/Stefan.Hoffmann


Wednesday, June 15, 2011 11:59 AM

You can use the RemoveAll() method for removing the objects in one list. To identify these objects use the Intersect() method.Microsoft MVP Office Access
https://mvp.support.microsoft.com/profile/Stefan.Hoffmann


Wednesday, June 15, 2011 12:15 PM

Hi, Do you want to remove duplications from both Lists?Mitja


Wednesday, June 15, 2011 2:40 PM

yes , i wann to remove duplicate from both list


Wednesday, June 15, 2011 4:20 PM

Using Stefan's suggestion

 

      List<string> list1 = new List<string>();
      List<string> list2 = new List<string>();

      list1.Add("item1");
      list1.Add("item2");
      list1.Add("item3");
      list1.Add("item4");

      list2.Add("item1");
      list2.Add("item3");
      list2.Add("item2");
      list2.Add("item5");

      IQueryable<string> intersertList = list1.AsQueryable().Intersect(list2);
      List<string> commonItem = intersertList.ToList();

      foreach (string item in commonItem)
      {
        list1.Remove(item);
        list2.Remove(item);
      }

This removes all common items from both the lists....

 

 

Regards, Krishnakant This answers, please mark as answered, if this helps please mark as helpful.