Harris Calvin
Harris Calvin

Reputation: 473

Counting words using LinkedList

I have a class WordCount which has string wordDic and int count. Next, I have a List.

I have ANOTHER List which has lots of words inside it. I am trying to use List to count the occurrences of each word inside List.

Below is where I am stuck.

class WordCount
{
string wordDic;
int count;

}


    List<WordCount> usd = new List<WordCount>();

    foreach (string word in wordsList)
    {
        if (usd.wordDic.Contains(new WordCount {wordDic=word, count=0 }))
           usd.count[value] = usd.counts[value] + 1;
        else
            usd.Add(new WordCount() {wordDic=word, count=1});

    }

I don't know how to properly implement this in code but I am trying to search my List to see if the word in wordsList already exists and if it does, add 1 to count but if it doesn't then insert it inside usd with count of 1.

Note: *I have to use Lists to do this. I am not allowed to use anything else like hash tables...*

Upvotes: 1

Views: 346

Answers (5)

Harrison
Harrison

Reputation: 3963

This is the answer before you edited to only use lists...btw, what is driving that requirement?

List<string> words = new List<string> {...};

// For case-insensitive you can instantiate with 
// new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
Dictionary<string, int> counts = new Dictionary<string, int>(); 
foreach (string word in words)
{
    if (counts.ContainsKey(word))
    { 
        counts[word] += 1;
    }
    else
    {
        counts[word] = 1;
    }
}

If you can only use lists, Can you use List<KeyValuePair<string,int>> counts which is the same thing as a dictionary (although I'm not sure it would guarantee uniqueness). The solution would be very similar. If you can only use lists the following will work.

        List<string> words = new List<string>{...};
        List<string> foundWord = new List<string>();
        List<int> countWord = new List<int>();
        foreach (string word in words)
        {
            if (foundWord.Contains(word))
            {
                countWord[foundWord.IndexOf(word)] += 1;
            }
            else
            {
                foundWord.Add(word);
                countWord.Add(1);
            }
        }

Using your WordCount class

        List<string> words = new List<string>{...};
        List<WordCount> foundWord = new List<WordCount>();
        foreach (string word in words)
        {
            WordCount match = foundWord.SingleOrDefault(w => w.wordDic == word);
            if (match!= null)
            {
                match.count += 1;
            }
            else
            {
                foundWord.Add(new WordCount { wordDic = word, count = 1 });
            }
        }

Upvotes: 2

user2802981
user2802981

Reputation:

You should use a Dictionary as its faster when using the "Contains" method.

Just replace your list with this

Dictionary usd = new Dictionary();

foreach (string word in wordsList)
{
    if (usd.ContainsKey(word.ToLower()))
       usd.count[word.ToLower()].count++;
    else
        usd.Add(word.ToLower(), new WordCount() {wordDic=word, count=1});

}

Upvotes: 0

Tu Tran
Tu Tran

Reputation: 1977

First, all of your class member is private, thus, they could not be accessed somewhere out of your class. Let's assume you're using them in WordCount class too.

Second, your count member is an int. Therefore, follow statement will not work:

usd.count[value] = usd.counts[value] + 1;

And I think you've made a mistype between counts and count.

To solve your problem, find the counter responding your word. If it exists, increase count value, otherwise, create the new one.

foreach (string word in wordsList) {
    WordCount counter = usd.Find(c => c.wordDic == word);
    if (counter != null) // Counter exists
        counter.count++;
    else
        usd.Add(new WordCount() { wordDic=word, count = 1 }); // Create new one
}

Upvotes: 0

Jason
Jason

Reputation: 1383

You can use Linq to do this.

static void Main(string[] args)
{

    List<string> wordsList = new List<string>()
    {
        "Cat",
        "Dog",
        "Cat",
        "Hat"
    };

    List<WordCount> usd = wordsList.GroupBy(x => x)
                                   .Select(x => new WordCount() { wordDic = x.Key, count = x.Count() })
                                   .ToList();
}

Upvotes: 1

Noctis
Noctis

Reputation: 11763

Use linq: Assuming your list of words :

string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "abacus","apple", "cheese" }; 

You can do:

var count =
   from word in words
   group word.ToUpper() by word.ToUpper() into g
   where g.Count() > 0
   select new { g.Key, Count = g.Count() }; 

(or in your case, select new WordCount()... it'll depend on how you have your constructor set up)...

the result will look like:

enter image description here

Upvotes: 0

Related Questions