I need to calculate how many times each keyword is reoccurring in a string, with sorting by highest number. What's the fastest algorithm available in .NET code for this purpose?
相关问题
- How to toggle on Order in ReactJS
- PHP Recursively File Folder Scan Sorted by Modific
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- What is the best way to do a search in a large fil
Simple algorithm: Split the string into an array of words, iterate over this array, and store the count of each word in a hash table. Sort by count when done.
EDIT: code below groups unique tokens with count
This is finally starting to make more sense to me...
EDIT: code below results in count correlated with target substring:
Results is now:
Original code below:
with grateful acknowledgement to this previous response.
Results from debugger (which need extra logic to include the matching string with its count):
Dunno about fastest, but Linq is probably the most understandable:
You can write this in a non-Linq-y way, but the basic premise is the same; split the string up into words, and count the occurrences of each word of interest.
You could break the string into a collection of strings, one for each word, and then do a LINQ query on the collection. While I doubt it would be the fastest, it would probably be faster than regex.