0

I have a string array that contains a list every string in that list contains 4 initials, I want to compare how many times the initials repeat themselves

If the initials are repeated I want to add those initials to a List<string> and count how many times the initial repeat using a List<int>

  • If ENFE appears in the array add that to List<string> and List<int> counts = 1

  • If ENFE appears again counts = 2

  • If CCOM appears add to List<string> and List<int> counts = 1....

This is the code i have

 foreach(string item in lstProgramas.Items)
        {
            
            if(tempDeptSiglas[1] == item)
            {
                deptSiglas.Add(tempDeptSiglas[1]);
                contador.Add(+1);
            }
        }

I'm getting that tempDeptSiglas is null

leomarrg1
  • 11
  • 3
  • 1
    I have tried my best to format this question. Please try a little harder to polish your question before posting next time. You will find you get a better response from people – TheGeneral Nov 05 '20 at 03:23
  • Does this make it clearer? @TheGeneral – leomarrg1 Nov 05 '20 at 03:28
  • I dont understand where the INITIALS will come from. But that code looks wrong. Loop variable i is never used. You always look at first item [1] in the array. – Prateek Shrivastava Nov 05 '20 at 03:30
  • you don't show where tempDeptSiglas is declared.... the problem with it being null is going to be outside the code you have posted – Keith Nicholas Nov 05 '20 at 03:34
  • You need to initialize `tempDeptSiglas`, seemingly its `null`, which means you haven't created it or put data in it – TheGeneral Nov 05 '20 at 03:35
  • this is how `public string[] tempDeptSiglas;` is declared, i fixed the code a little bit i think it shows better what i want to do. – leomarrg1 Nov 05 '20 at 03:44
  • I think my problem is that `tempDeptSiglas` array is not getting the string it's supposed to – leomarrg1 Nov 05 '20 at 04:04

1 Answers1

0

Attempting to answer "Some question" without fully understanding your problem.

In the below code - the Dictionary "should" contain counts for each Initials.

 List<string> initials = new List<string>();
 // Some way to fill initials collection
 List<string> searchInThis = new List<string>();
 // Some way to fill searchInThis collection
 Dictionary<string, int> initialCounts = new Dictionary<string, int>();

 searchInThis.ForEach(t =>
 {
    initials.ForEach(i =>
    {
       if(System.Text.RegularExpressions.Regex.IsMatch(t, i))
       {
          if (initialCounts.TryGetValue(i, out int count))
          {
             count++;
             initialCounts[i] = count;
          }
          else
          {
             initialCounts[i] = 1;
          }
       }
    });
 });

Obvisouly this can be optimized - with domain knowledge like if one Initial matches - we dont need to test other intials (duh..!!) and other stuff.

Prateek Shrivastava
  • 1,784
  • 1
  • 8
  • 17
  • What do you don't understand, english is not my first lenguague i can try to explain it better – leomarrg1 Nov 05 '20 at 04:05
  • You have 1 list of Initials Ex: {"Hell", "Well", "Xell"}. Now where do you have to search & count the initials? Is it a different list or what? You should have added few examples of Data. – Prateek Shrivastava Nov 05 '20 at 05:22