0

I have a data structure like this:

List<Dictionary<string, string[]>>

Key of dictionary is not important, but Value array is. I need to count how many values of strings appeared in it together. For example, string array contains these records:

"one"
"one", "two", "four"
"two", "three"
"two", "one", "four"
"one"
"one", "ten"
"two", "four", "one"

The result should look like this:

Value                    Occurrence
"one"                     2
"one", "two", "four"      3
"two", "three"            1
"one", "ten"              1

What I thought and tried to do, was to:

  • create a new Dictionary to hold results
  • loop through the List using foreach
  • sort string array in List<Dictionary>
  • check if that string array exists in new Dictionary
  • if it doesn't, add it in value and put 1 in key
  • if it does, increment key

Other than the fact whether it will work or not, I am not even sure if that is an efficient way to do this.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Farhan
  • 2,535
  • 4
  • 32
  • 54
  • 1
    Use GroupBy of a composed String of the ordered items (or use GroupBy that accepts an IEqualityComparer and implement that) and then Count for each Group. Once the grouping is done it's no different than a 'normal' count of groups. – user2864740 Jun 28 '19 at 01:47
  • With the composed strings, imagine that the input is effectively flattened as "one", "four-one-two", "three-two", "four-one-two" (note stable ordering for string-equality), .. using the IEqualityComparer form can effectively pass this, while otherwise serving the same goal (ensuring equality of keys to determine 'a group'). Remember that, by default, arrays in C# are *not usefully* comparable, Enumerable.SequenceEquals can be used in this case (still requires ordering). – user2864740 Jun 28 '19 at 01:50
  • There are tons of post on computing frequency... As soon as you figure out what "equals" means for your values you can apply code in duplicate... You can either use https://stackoverflow.com/questions/494224/how-do-you-determine-if-two-hashsets-are-equal-by-value-not-by-reference or other ways to compare your string arrays as suggested by @user2864740. – Alexei Levenkov Jun 28 '19 at 02:25

0 Answers0