0

I have list of usernames (string). Each name cn[i] will have several skills attached to it:

cn[0] has: "text1, text2, text3"
cn[1] has: "text2, text4, text6"
cn[2] has: "text6, text8"
cn[2] has: "text11, text8, text1, text4, text2"

etc.

Now I need to count how many skills in total and each skill how many people have. So I think it will contain following steps:

1. add text1, text2, ... to an array (I don't know how to get each string and get rid of the comma "'")
2. suppose we have 

    string[] stringArray = { "text1", "text2", "text3", "text4", "text6", ... };

we can check the frequency by:

foreach (string x in stringArray)
{
    if (x.Contains(stringToCheck))
    {
        // increase the number of count
    }
}

3. I don't know how to stick that count number to each skill then later we can display it. I am thinking of something like Map map = new HashMap();

4
  • I gather you are looking for two things. The first is the number of occurrences of each skill across all users. The second is the same figure, but by user. Is this correct? Commented Jun 9, 2014 at 4:32
  • By skills in total, do you mean how many distinct skills there are? Commented Jun 9, 2014 at 4:46
  • yes, distinct skills, no repeat skill (string). Commented Jun 9, 2014 at 6:39
  • Please rewrite your question more explicitly. It is unclear what you are trying to achieve. By the way, the thing that's like a HashMap is Dictionary in C#. What would it mean to count skill occurrence within each user? Aren't a single user's skills distinct already? Is a filtering step required? Be clear! Be explicit! I'll make another attempt to help at that point. Good luck! Commented Jun 9, 2014 at 14:51

1 Answer 1

1

You can use the GroupBy and ToDictionary extension methods found in System.Linq to perform the task:

using System.Linq;

var frequency = cn
    .SelectMany(u => u.Split(new string[] { ", " }, StringSplitOptions.None))
    .GroupBy(s => s)
    .ToDictionary(g => g.Key, g => g.Count());

var numberOfSkills = frequency.Count;

var numberOfUsersWithSkillOne = frequency["SkillOne"];
Sign up to request clarification or add additional context in comments.

12 Comments

I didn't have string Array at step 2. How to form it ? And I don't see any place that it will store frequency to each skill in your code. sorry if I miss some points...
I've offered a simplified solution you might prefer. Please accept the answer if you like the solution, or feel free to ask more questions.
Yes, certainly I like the simpler answer. Let me check your solution. How could I form the skill sets from each user skill set? Only after that I can count frequency right ?
Do you mean: (u => u.Split(new string[] { ", " }, where "u" instead of "s"
@BobVale OP was referring to a real typo I made. I had u => s... instead of u => u.... I've already edited my answer, is all.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.