locked
How do i remove duplicated items from a List<string> ? RRS feed

  • Question

  • for (int i = 0; i < links.Count; i++)
                {
                    int f = links[i].IndexOf("#");
                    string test = links[i].Substring(0, f);
                    FilteredLinks.Add(test);
                }
                var duplicates = FilteredLinks.GroupBy(a => a).SelectMany(ab => ab.Skip(1).Take(1)).ToList();

    In duplicates i see in this case 48 items that are the same as already have in FilteredLinks.

    But FilteredLinks keep growing with the same items. ( This list i called by a timer each X seconds ).

    How can i check for duplicates items so it won't even add to the FilteredLinks in first place ?

    Thursday, July 3, 2014 11:39 AM

Answers

  • Hi,

    Simplest way is to use Distinct();

        class Program
        {
            static void Main(string[] args)
            {
                List<string> list = new List<string>() { "hello", "hello", "world" };
    
                foreach (string item in list.Distinct())
                    Console.WriteLine(item);
            }
        }

    Regards,

    Nick

    Thursday, July 3, 2014 11:47 AM

All replies

  • Hi,

    Simplest way is to use Distinct();

        class Program
        {
            static void Main(string[] args)
            {
                List<string> list = new List<string>() { "hello", "hello", "world" };
    
                foreach (string item in list.Distinct())
                    Console.WriteLine(item);
            }
        }

    Regards,

    Nick

    Thursday, July 3, 2014 11:47 AM
  • If you don't mind the order of the links, you can use the Hashset<T> class:

    HashSet<String> FilteredLinks = new HashSet<String>(StringComparer.InvariantCultureIgnoreCase);
    for (int i = 0; i < links.Count; i++)
    {
        int f = links[i].IndexOf("#");
        string test = links[i].Substring(0, f);
        if (!FilteredLinks.Add(test))
        {
            Console.WriteLine("Item {0} already exists", test);
        }
    }

    This will check for existance of the string you're adding in the Add() method which returns true if the value was added successfully and false if it had already been added.

    Thursday, July 3, 2014 12:09 PM
  • Hello,

    In regards to Nick's response, here is a nice tutorial from Microsoft.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, July 3, 2014 1:35 PM