Created
March 10, 2021 06:53
-
-
Save codedjinn/ad8d278c549608fd1a5ac4a1c4785d46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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