Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualAmzBee

Posted 06 March 2013 - 08:38 AM

OK maybe I am definately looking too far into this lol, I've found another method that's even faster than method A, except it does what I was trying to avoid by using a dictionary as follows:

 

static Dictionary<int, int> table = new Dictionary<int, int>();

static int ModeE(int[] items)
{
    table.Clear();
    for (int i = 0; i < items.Length; i++)
    {
        var item = items[i];
        if (!table.ContainsKey(item)) table.Add(item, 0);
        table[item]++;
    }
    return table.First(t => t.Value == table.Max(u => u.Value)).Key;
}

 

Does anyone see anything here that I'm missing that could cause unwanted garbage?

 

Aimee


#2AmzBee

Posted 06 March 2013 - 07:17 AM

OK maybe I am definately looking too far into this lol, I've found another method that's even faster than method A, except it does what I was trying to avoid by using a dictionary as follows:

 

static Dictionary<int, int> table = new Dictionary<int, int>();

static int ModeE(int[] items)
{
    table.Clear();
    for (int i = 0; i < items.Length; i++)
    {
        var item = items[i];
        if (!table.ContainsKey(item)) table.Add(item, 0);
        table[item]++;
    }
    return table.Max(t => t.Key);
}

 

Does anyone see anything here that I'm missing that could cause unwanted garbage?

 

Aimee


#1AmzBee

Posted 06 March 2013 - 07:17 AM

OK maybe I am definately looking too far into this lol, I've found another method that's even faster than method A, except it does what I was trying to avoid by using a dictionary as follows:

 

static Dictionary<int, int> table = new Dictionary<int, int>();

static int ModeE(int[] items)
{
    table.Clear();
    for (int i = 0; i < items.Length; i++)
    {
        var item = items[i];
        if (!table.ContainsKey(item)) table.Add(item, 0);
        table[items[i]]++;
    }
    return table.Max(t => t.Key);
}

 

Does anyone see anything here that I'm missing that could cause unwanted garbage?

 

Aimee


PARTNERS