Skip to content

Instantly share code, notes, and snippets.

@codedjinn
Created March 10, 2021 06:53
Show Gist options
  • Select an option

  • Save codedjinn/ad8d278c549608fd1a5ac4a1c4785d46 to your computer and use it in GitHub Desktop.

Select an option

Save codedjinn/ad8d278c549608fd1a5ac4a1c4785d46 to your computer and use it in GitHub Desktop.
class PriorityQueue
{
private Dictionary<int, int> _frequencies;
public PriorityQueue()
{
_frequencies = new Dictionary<int, int>();
}
public void Add(int item)
{
if (_frequencies.ContainsKey(item))
{
_frequencies[item]++;
}
else
{
_frequencies.Add(item, 1);
}
}
public int Pop()
{
int max = 0;
int key = -1;
foreach (var pair in _frequencies)
{
if (pair.Value > max)
{
max = pair.Value;
key = pair.Key;
}
}
_frequencies.Remove(key);
return key;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment