How to Remove Duplicate Words in String using C#

No.of Views1314
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  13 Dec 2010 01:12:05
Tag : CSharp , Miscellaneous
I have read in forums there are many users asking questions about removing duplicates words from the string; remove duplicate elements from the ArrayList.So this snippet shows how to remove the repeating words in string using c#.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

 Introduction

I have read in forums there are many users asking questions about removing duplicates words from the string; remove duplicate elements from the ArrayList. I have already post a snippets for remove the duplicate elements from ArrayList using C# . So this snippet shows how to remove the repeating words in string using c#.

Implementation

There are many ways to do this, but I’m using Dictionary object to keep non duplicate words as key and parse string in loop and add into another string. Finally return duplicate words removed string.

C# Code

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class StringUtility
    {

        public static string RemoveRepeatWords(string source)
        {

            Dictionary<string, bool> listofUniqueWords = new Dictionary<string, bool>();
            StringBuilder destBuilder = new StringBuilder();
            string[] spilltedwords = source.Split(new char[] { ' ', ',', ';', '.' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in spilltedwords)
            {
                string lower = item.ToLower();
                if (!listofUniqueWords.ContainsKey(lower))
                {
                    destBuilder.Append(item).Append(' ');
                    listofUniqueWords.Add(lower, true);
                }
            }
            
            return destBuilder.ToString().Trim();
        }
    }
}

How to use ?

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string source = "This is test string composed by RRaveen demostration purpose. if this string has duplicate words this method will remove and reprint.";
            Console.WriteLine("{0} :{1}", "source", source);
            Console.WriteLine("{0} :{1}", "Destionation", StringUtility.RemoveRepeatWords(source));
            Console.ReadLine();
        }
    }
}

Output

source :This is test string composed by RRaveen demostration purpose. if this string has duplicate words this method will remove and reprint.

Destionation :This is test string composed by RRaveen demostration purpose if has duplicate words method will remove and reprint

hopes help.thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top