AYETY
AYETY

Reputation: 710

remove duplicate words from string in C#

here is my code:

class Program
    {
        static void Main(string[] args)
        {
            string sentence = string.Empty;
            sentence = Console.ReadLine();
            string[] sent = sentence.Split(' ');
            //to be sorted alphabetically
            var x =
                from k in sent
                orderby k
                select k;

            foreach (string s in x)
            {
                    Console.WriteLine(s.ToLower());
            }

            Console.ReadLine();
        }
    }

is there any method to find and remove duplicate words or I should make my own method?

Upvotes: 3

Views: 20088

Answers (5)

Sagar Jadhav
Sagar Jadhav

Reputation: 129

Simple Remove Duplicate words form string by using for loop. try this it's working fine.

string SetenceString = "red white black white green yellow red red black white";
        string[] data = SetenceString.Split(' ');
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            string temp = " ";
            if (i == 0)
            {
                temp = data[i].ToString();
                sb = sb.Append(temp + " ");

            }
            else
            {
                for (int j = 1; j < data.Length; j++)
                {
                    string temp2 = data[j].ToString();
                    string strnew = sb.ToString();
                    string[] Kdata = strnew.Split(' ');
                    bool isnoduplicate = false;
                    for (int k = 0; k < Kdata.Length; k++)
                    {
                        string temp3 = Kdata[k].ToString();

                      if (temp3 != "")
                        {
                            if (temp2 == temp3)
                            {
                                isnoduplicate = false;
                                break;
                            }
                            else
                            {
                                isnoduplicate = true;
                            }
                        }
                    }
                    if (isnoduplicate)
                    {
                        sb = sb.Append(temp2 + " ");
                    }

                }
            }
        }
        Console.WriteLine(sb);
        Console.ReadKey();

Upvotes: 0

Tim S
Tim S

Reputation: 2329

This should do everything you're asking:

class Program
{
    static void Main(string[] args)
    {
        string sentence = string.Empty;
        sentence = Console.ReadLine();

        var sent = sentence
            .Split(' ')
            .Distinct()
            .OrderBy(x => x);

        foreach (string s in sent)
        {
            Console.WriteLine(s.ToLower());
        }

        Console.ReadLine();
    }
}

Hope it helps!

Upvotes: 0

John Koerner
John Koerner

Reputation: 38077

Use Distinct:

foreach (string s in x.Distinct())
{
        Console.WriteLine(s.ToLower());
}

Upvotes: 4

p.s.w.g
p.s.w.g

Reputation: 149020

You could use Linq's Distinct extension method:

var sent = sentence.Split(' ').Distinct();

You can also use this to ignore the case of strings when comparing them—e.g. "WORD" and "word" would be considered duplicates:

var sent = sentence.Split(' ').Distinct(StringComparer.CurrentCultureIgnoreCase);

Upvotes: 11

empi
empi

Reputation: 15881

Use System.Linq Distinct:

foreach (string s in x.Distinct())

Upvotes: 8

Related Questions