Skip to main content
added 617 characters in body
Source Link
BlueTrin
  • 201
  • 1
  • 5

Your code is using a wildcard (' '), wildcards are generally bad as someone else pointed. As a general advice, I would recommend you to avoid writing algorithms based on adding a wildcard when it can be part of the input.

EDIT: this works only if the string does not special UNICODE values as mjolka mentioned, char can be 16-bits in C#.

EDIT: this works only if the string does not special UNICODE values as mjolka mentioned, char can be 16-bits in C#.

Your code is using a wildcard (' '), wildcards are generally bad as someone else pointed. As a general advice, I would recommend you to avoid writing algorithms based on adding a wildcard when it can be part of the input.

EDIT: this works only if the string does not special UNICODE values as mjolka mentioned, char can be 16-bits in C#.

added 617 characters in body
Source Link
BlueTrin
  • 201
  • 1
  • 5

On the pro side, it is much faster because accessing an array is constant in time, there is literally no search or indexing, on the con side it uses a static array of 255 int. Also in C#, if you follow the guidelines of Resharper or StyleCop, they will recommend you to use var when you can and to not use '_' prefixing:

    public static int GetMaximumOccurence(string str)
    {
        var countPerChar = new Dictionary<char, int>();
        int maxcount = 0;
        foreach (char c in str)
        {
            int newVal;
            if (!countPerChar.ContainsKeyTryGet(c, newVal))
                newVal = (countPerChar[c] = 1);
            else
                newVal = (countPerChar[c] += 1);
            maxcount = Math.Max(maxcount, newVal);
        }
        return maxcount;
    }

dss539 suggestion leads to:

    public static int GetMaximumOccurence(string str)
    {
        var countPerChar = new Dictionary<char, int>();
        int maxcount = 0;
        foreach (char c in str)
        {
            int newVal;
            countPerChar.TryGetValue(c, out newVal);        // NewVal is set to 0 if the value is not found
            maxcount = Math.Max(maxcount, (countPerChar[c] = newVal + 1));
        }
        return maxcount;
    }

On the pro side, it is much faster, on the con side it uses a static array of 255 int. Also in C#, if you follow the guidelines of Resharper or StyleCop, they will recommend you to use var when you can and to not use '_' prefixing:

    public static int GetMaximumOccurence(string str)
    {
        var countPerChar = new Dictionary<char, int>();
        int maxcount = 0;
        foreach (char c in str)
        {
            int newVal;
            if (!countPerChar.ContainsKey(c))
                newVal = (countPerChar[c] = 1);
            else
                newVal = (countPerChar[c] += 1);
            maxcount = Math.Max(maxcount, newVal);
        }
        return maxcount;
    }

On the pro side, it is much faster because accessing an array is constant in time, there is literally no search or indexing, on the con side it uses a static array of 255 int. Also in C#, if you follow the guidelines of Resharper or StyleCop, they will recommend you to use var when you can and to not use '_' prefixing:

    public static int GetMaximumOccurence(string str)
    {
        var countPerChar = new Dictionary<char, int>();
        int maxcount = 0;
        foreach (char c in str)
        {
            int newVal;
            if (!countPerChar.TryGet(c, newVal))
                newVal = (countPerChar[c] = 1);
            else
                newVal = (countPerChar[c] += 1);
            maxcount = Math.Max(maxcount, newVal);
        }
        return maxcount;
    }

dss539 suggestion leads to:

    public static int GetMaximumOccurence(string str)
    {
        var countPerChar = new Dictionary<char, int>();
        int maxcount = 0;
        foreach (char c in str)
        {
            int newVal;
            countPerChar.TryGetValue(c, out newVal);        // NewVal is set to 0 if the value is not found
            maxcount = Math.Max(maxcount, (countPerChar[c] = newVal + 1));
        }
        return maxcount;
    }
added 119 characters in body
Source Link
BlueTrin
  • 201
  • 1
  • 5

If your string contains UNICODE characters, you will have to revert to a dictionary approach or use dss539 solution which will workor increase the array size to be able to contain 2^16 values to make itwork in every case. Please note that 2^16 int start to be a significant amount of memory for just counting references ...

A Dictionary approach would be:

    public static int GetMaximumOccurence(string str)
    {
        var countPerChar = new Dictionary<char, int>();
        int maxcount = 0;
        foreach (char c in str)
        {
            int newVal;
            if (!countPerChar.ContainsKey(c))
                newVal = (countPerChar[c] = 1);
            else
                newVal = (countPerChar[c] += 1);
            maxcount = Math.Max(maxcount, newVal);
        }
        return maxcount;
    }

But I find dss539 answer is superior in the light of knowing that char is 16bit in C#.

If your string contains UNICODE characters, you will have to revert to a dictionary approach or use dss539 solution which will work in every case.

If your string contains UNICODE characters, you will have to revert to a dictionary approach or use dss539 solution or increase the array size to be able to contain 2^16 values to make itwork in every case. Please note that 2^16 int start to be a significant amount of memory for just counting references ...

A Dictionary approach would be:

    public static int GetMaximumOccurence(string str)
    {
        var countPerChar = new Dictionary<char, int>();
        int maxcount = 0;
        foreach (char c in str)
        {
            int newVal;
            if (!countPerChar.ContainsKey(c))
                newVal = (countPerChar[c] = 1);
            else
                newVal = (countPerChar[c] += 1);
            maxcount = Math.Max(maxcount, newVal);
        }
        return maxcount;
    }

But I find dss539 answer is superior in the light of knowing that char is 16bit in C#.

added 119 characters in body
Source Link
BlueTrin
  • 201
  • 1
  • 5
Loading
Source Link
BlueTrin
  • 201
  • 1
  • 5
Loading