Click here to Skip to main content
15,666,947 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
HI,

I need to remove duplicate strings of list string

Can anyone help me in solving this


Thanks
John
Posted
Updated 5-Jul-17 18:07pm
Comments
Vedat Ozan Oner 12-Feb-14 4:31am    
see here: http://www.dotnetperls.com/list and here: http://msdn.microsoft.com/en-us/library/cc165449.aspx. what to do find these links:
1- open google
2- search for c# list
3- search for compare strings c#

Use it:

List<string> distinct = list.Distinct().ToList();
 
Share this answer
 
Use the Distinct[^] method from LINQ.

e.g.
C#
IList<string> list = new List<string>
	{
		"a",
		"b",
		"A",
		"c",
		"d"
	};

Console.WriteLine(String.Join(", ", list)); // prints a, b, A, c, d

// Case sensitive comparison
list = list.Distinct().ToList();
Console.WriteLine(String.Join(", ", list)); // prints a, b, A, c, d

// In case you want to use case insensitive, invariant culture comparison, use this one. Different comparers are available: http://msdn.microsoft.com/en-us/library/system.stringcomparer(v=vs.110).aspx
list = list.Distinct(StringComparer.InvariantCultureIgnoreCase).ToList();
Console.WriteLine(String.Join(", ", list)); // prints a, b, c, d
 
Share this answer
 
v2
Try out with code

C#
List<string> list = new List<string>();
            list.Add("1");
            list.Add("2");
            list.Add("5");
            list.Add("5");
            list.Add("2");
            list.Add("3");
            list.Add("7");

            // Get distinct elements and convert again into a list.
            List<string> distinct = list.Distinct().ToList();

            foreach (string value in distinct)
            {
                Console.WriteLine("Distinct : {0}", value);
            }
 
Share this answer
 
v3
you can using following code
C#
String[] a = { "abc", "xyz", "abc", "def", "ghi", "asdf", "ghi", "xd", "abc" };

           String[] b = a.Distinct<String>().ToArray();
 
Share this answer
 
Please have a look on the bellow link :
C# Remove Duplicates[^]
 
Share this answer
 
XML
static List<string> removeDuplicates(List<string> inputList)
{
    Dictionary<string, int> uniqueStore = new Dictionary<string, int>();
    List<string> finalList = new List<string>();
    foreach (string currValue in inputList)
    {
        if (!uniqueStore.ContainsKey(currValue))
        {
            uniqueStore.Add(currValue, 0);
            finalList.Add(currValue);
        }
    }
    return finalList;
}



Regards,
Praveen Nelge
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900