4 Replies - 1185 Views - Last Post: 27 January 2013 - 01:34 PM Rate Topic: -----

#1 Delilah Demented  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-January 10

Remove duplicates from an array string

Posted 26 January 2013 - 06:21 PM

I am trying to remove the duplicate words from a sentence entered by the user without punctuation. I believe I am over thinking this. If the user types in "that cat is black and this cat is yellow", then it should display:
that
black
and
this
yellow

since cat and is are used twice. Instead, it just shows each word once and doesn't remove the two duplicates. Any help is greatly appreciated!

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            {   
                // Creates list of string
                List<string> list = new List<string>();

                // Writes for sentence
                Console.Write("Enter a sentence with no punctuation: ");

                // Converts console into string
                string sentence = (Console.ReadLine());

                // Splits string into array
                string[] words = sentence.Split();

                // Writes array to list
                //for (int i = 0; i < words.Length; i++)
                //{
                //    list.Add(words[i]);
                //}

  

                //Writes words
                foreach (var element in words)
                {
                    if (!list.Contains(element))
                        list.Add(element);
                }

                // Sorts words
                var sort =
                    from word in list
                    let Upper = word.ToUpper()
                    orderby Upper
                    select Upper;

                foreach (var element in sort)
                {
                    Console.WriteLine(element);
                }


                Console.ReadLine();
            }
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Remove duplicates from an array string

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Remove duplicates from an array string

Posted 26 January 2013 - 06:30 PM

Currently because you have commented out the section that adds the words to the list, all the words will be added as list.Contains(word) will always return false (as there are not words in the list).

That being said if you uncomment it, it still won't work as you would simply be adding the same word into the list again.

I quick and easy way would be using a HashSet instead of a list. Simply create an instance, and add all the words to it. The HashSet won't add the new element if it already exists.

Alternatively you can use the .Distinct() method -

var noDuplicates = words.Distinct().ToList();


Or if you want to stick with your current code, uncomment the bit where you add the words and use a new list to add the unique words to. Instead of doing .Contains on the existing list, do it on the new unique words list that you then add to.

This post has been edited by Ryano121: 26 January 2013 - 06:31 PM

Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,673
  • Joined: 02-June 10

Re: Remove duplicates from an array string

Posted 26 January 2013 - 06:45 PM

If you used a Dictionary<string, int> and tried to enter each word as you came across it any duplicates would be dropped.
The string would be the key and the int could be the number of times it appears in the sentence.

Why just drop the dups when you can do more than asked for with less work?
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Remove duplicates from an array string

Posted 27 January 2013 - 04:56 AM

First, the sort seems to be getting you farther from your goal.

Your program, slightly reorganized:
class Program {
	static void DisplayResults(string sentence) {
		string[] words = sentence.Split();
		List<string> list = new List<string>();
		foreach (var element in words) {
			if (!list.Contains(element))
				list.Add(element);
		}
		/*
		var sort =
			from word in list
			let Upper = word.ToUpper()
			orderby Upper
			select Upper;
		foreach (var element in sort) {
			Console.WriteLine(element);
		}
		*/
		foreach (var element in list) {
			Console.WriteLine(element);
		}
	}

	static void Main(string[] args) {
		// Console.Write("Enter a sentence with no punctuation: ");
		// DisplayResults(Console.ReadLine());
		DisplayResults("that cat is black and this cat is yellow");
		Console.ReadLine();
	}
}



( I hate retyping on the console, don't you? )

Results:
that
cat
is
black
and
this
yellow



Knowing that List supports a remove method, you can remove the value from the already processed list when you hit a duplicate. The only trick is not to loose words already marked as dups.

I'd start with something like:
List<string> printList = new List<string>();
List<string> dupList = new List<string>();
foreach (var element in sentence.Split()) {
	if (!dupList.Contains(element)) {
		if (printList.Contains(element)) {



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#5 Delilah Demented  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-January 10

Re: Remove duplicates from an array string

Posted 27 January 2013 - 01:34 PM

Thanks for the replies, I've got it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1