-2

I am pretty new to C# and I would like to know how more experienced programmers would solve this issue. In this code the is a private const string with a bunch of numbers and letters. I need my program to find which number occurs the most frequently and how many times it occurs and spit out the results.

public class Program
{
private const string FileContents = "\"12345.56789\",\"12345.56789\",\"12345.56789\",\"12345.56789\"";
public static void Main()
{
}
}
DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • What do you consider "a number" here? A single digit? Or the whole thing between \" and \"? – Fildor Feb 18 '22 at 10:57

2 Answers2

1

You can use GroupBy on each number/word and then check count of element in each group,

private const string FileContents = "\"12345.56789\",\"12345.56789\",\"12345.56789\",\"12345.56789\"";
var mostFrequent = FileContents
    .Split(',')
    .GroupBy(x => x)
    .Select(x => new {Key = x.Key, Count = x.Count()})
    .OrderByDescending(x => x.Count)
    .FirstOrDefault();

mostFrequent variable will contain either number or word(letters) with highest occurrences in the given const string.


If you want only number not a word, then you have to filter split array into number array, like below

var mostFrequentNumber = FileContents
    .Split(',')
    .Select(x => x.Trim("\""))  //Remove leading and trailing \"
    .Where(x => float.TryParse(x, out _)) //Check string is numeric or not
    .GroupBy(x => x)
    .Select(x => new {Key = x.Key, Count = x.Count()})
    .OrderByDescending(x => x.Count)
    .FirstOrDefault();
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • 1
    you clould use `.Where(x => char.IsDigit(x))` instead of `.Split(",") .Select(x => x.Trim("\"")) //Remove leading and trailing \" .Where(x => float.TryParse(x, out float number))` – valentin Feb 18 '22 at 10:47
  • The quotes for `","` need to be single quotes: `','` to be a char instead of a string (and for `"\""`). You could use a [discard](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/discards) here: `float.TryParse(x, out _)`. And the dot is missing before `FirstOrDefault()`. – Andrew Morton Feb 18 '22 at 10:55
  • 1
    @AndrewMorton. thanks for your comment, I fixed issue pointed by you and used discard which I was not aware of . Thanks for the link. – Prasad Telkikar Feb 18 '22 at 10:58
  • @valentin, we can't use `.Where(x => char.IsDigit(x))` because numbers are decimal in nature. It contains decimal number – Prasad Telkikar Feb 18 '22 at 10:59
  • @valentin, if you have better approach than mine, I am happy to see it – Prasad Telkikar Feb 18 '22 at 11:19
0

You can make this following these steps:

  1. Parse the string into the collection of numbers
  2. Group numbers (group should contain the number itself and the count for how many times it occurs)
  3. Order groups by the number of occurrences (in descending order) and take the first one (hence the one with the most number of occurrences)
  4. Extract the number from the group
satma0745
  • 667
  • 1
  • 7
  • 26