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!
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();
}
}
}
}