0

This function counts how often letter occur in a given string and puts that in an array (index is ascii-number of letter and value ist counted occurrences). Now I need to return both the letter (which it already does) and the value. Just by reading online I couldn't figure out how to use ref and alternatives to do that.

static char MostCommonLetter(string s)
    {
        int[] occurrances = new int[255];
        for (int i = 0; i < s.Length; i++)
        {
            if (char.IsLetter(s[i]))
            {
                int ascii = (int)s[i];
                occurrances[ascii]++;
            }
        }
        char maxValue = (char)Array.IndexOf(occurrances, occurrances.Max());
        return maxValue;
    }

4 Answers 4

4

In C# 7 and above, Value Tuples are your best bet. You could define your function as follows:

static (char letter, int occurrences) MostCommonLetter(string s)
{
    int[] occurrences = new int[255];
    for (int i = 0; i < s.Length; i++)
    {
        if (char.IsLetter(s[i]))
        {
            int ascii = (int)s[i];
            occurrances[ascii]++;
        }
    }
    char letter = (char)Array.IndexOf(occurrences, occurrences.Max());
    return (index: letter, occurrences: occurrences);
}

You could then reference the output like so:

var (index, occurrences) = MostCommonLetter(yourString);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the "out" parameter to return additional parameters from the function.

    static char MostCommonLetter(string s, out int maxOccurrance)
    {
        int[] occurrances = new int[255];
        for (int i = 0; i < s.Length; i++)
        {
            if (char.IsLetter(s[i]))
            {
                int ascii = (int)s[i];
                occurrances[ascii]++;
            }
        }

        maxOccurrance = occurrances.Max();
        char maxValue = (char)Array.IndexOf(occurrances, maxOccurrance);

        return maxValue;
    }

    //...

    // In C# 7 and above you can call it like that
    var c = MostCommonLetter("abccd", out int maxOccurrance);

    //// In older version of C# you should just declare out variable before use it
    //int maxOccurrance;
    //var c = MostCommonLetter("abccd", out maxOccurrance);

Comments

0

The best and flexible way to do what you need in C# is by using a struct.

Define a struct in this way and use it to return multiple results at same time (a struct can even contain function… you can see those structs as lightier classes):

namespace YourApp.AnyNamespace {

    // Other things

    public struct SampleName
    {
        public char mostCommon;
        public int occurancies;
    }
}

2 Comments

This for sure isn't "The best and flexible way"
@SlavenTojić anyway, another user reminded about tuples, added in C# 7, that are even easier to use.
0

An alternative solution would be using LINQ :

string str = "Hello World!";

var result = str.GroupBy(c => c)
                .Select(group => new { Letter = group.Key, Count = group.Count() })
                .OrderByDescending(x => x.Count)
                .First();

char letter = result.Letter;
int count = result.Count; 

letter = 'l'

count = 3

2 Comments

Ah yes, didn't read it… but now I can't delete the downvote, unless you edit the answer, says StackOverflow.
Down Vote removed. I beg sorry. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.