Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3345296
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:10:09+00:00 2026-05-18T01:10:09+00:00

I need to calculate how many times each keyword is reoccurring in a string,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:10:10+00:00Added an answer on May 18, 2026 at 1:10 am

    EDIT: code below groups unique tokens with count

    string[] target = src.Split(new char[] { ' ' });
    
    var results = target.GroupBy(t => new
    {
        str = t,
        count = target.Count(sub => sub.Equals(t))
    });
    

    This is finally starting to make more sense to me…

    EDIT: code below results in count correlated with target substring:

    string src = "for each character in the string, take the rest of the " +
        "string starting from that character " +
        "as a substring; count it if it starts with the target string";
    string[] target = {"string", "the", "in"};
    
    var results = target.Select((t, index) => new {str = t, 
        count = src.Select((c, i) => src.Substring(i)).
        Count(sub => sub.StartsWith(t))});
    

    Results is now:

    +       [0] { str = "string", count = 4 }   <Anonymous Type>
    +       [1] { str = "the", count = 4 }  <Anonymous Type>
    +       [2] { str = "in", count = 6 }   <Anonymous Type>
    

    Original code below:

    string src = "for each character in the string, take the rest of the " +
        "string starting from that character " +
        "as a substring; count it if it starts with the target string";
    string[] target = {"string", "the", "in"};
    
    var results = target.Select(t => src.Select((c, i) => src.Substring(i)).
        Count(sub => sub.StartsWith(t))).OrderByDescending(t => t);
    

    with grateful acknowledgement to this previous response.

    Results from debugger (which need extra logic to include the matching string with its count):

    -       results {System.Linq.OrderedEnumerable<int,int>}    
    -       Results View    Expanding the Results View will enumerate the IEnumerable   
            [0] 6   int
            [1] 4   int
            [2] 4   int
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate (in R) the number of positive and negative runs in
I need to calculate the net total of a column-- sounds simple. The problem
I need to calculate the number of FULL month in SQL, i.e. 2009-04-16 to
I am looking to calculate how many times people have viewed my users profiles
I need to calculate the time till Christmas in milliseconds. I'm using joda time
I need to calculate the total of an invoice. This invoice is created with
I need to calculate the distance (in meters and miles) between two coordinates given
I need to calculate date (year, month, day) which is (for example) 18 working
I need to calculate the within and between run variances from some data as
I need to calculate the quantity of nights (stay at a hotel) from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.