X
X
Xveeder2018-05-13 15:05:13
C++ / C#
Xveeder, 2018-05-13 15:05:13

How to extract only repeating N times elements of an array in C#?

Good afternoon, the task is such. There is an array of 1.5 million elements, each element contains a string.
These lines are repeated, and I need to extract from this array only those values ​​that occur at least N times in the array.
For example, the strings "foo", "bar", and "new" occur, say, 3 times in the array (each), you need to transfer them to a new array, but not all occurrences, but only one that will say (yeah, dudes , in that array there are N more people like me, but here I am alone).
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
d-stream, 2018-05-13
@d-stream

Linq .GropBy()

K
Kushin, 2018-05-19
@Kushin

Alternatively, through a dictionary
Initializing the dictionary itself
Enumeration of the original array of strings with an entry in the dictionary of the string and the number of its repetitions

foreach (var item in StrArr)                                                            
            {
                if (!StringCount.ContainsKey(item))
                    StringCount.Add(item, 1);
                else StringCount[item] += 1;
            }

The output is a key in the form of a string and a value in the form of its repetitions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question