Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am maintaining a dictionary that contains thee keywords and its no of occurrences. but i want to get top 5 max values from it. how to accomplish. kindly assist me in this regard. thanks.
Posted

I'm assuming that your keywords in the string & no. of occurrences in the int.

C#
var myDict = new Dictionary<string,>
                            {{"one", 1}, {"four", 4}, {"two", 2}, {"five", 3}, {"seven", 6}, {"six", 4}};

           var sortedDict = (from entry in myDict orderby entry.Value descending select entry)
               .ToDictionary(pair => pair.Key, pair => pair.Value).Take(5);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Oct-12 2:03am    
My 4. Please see my comment to solution 2.
--SA
Sushil Mate 25-Oct-12 2:27am    
Thanks for your reply & vote. Read your comment. can we get the best optimal solution by that way, I'm not sure. it should be optimal in the terms of time & space.
Direct us here :) example would be great.
Sergey Alexandrovich Kryukov 25-Oct-12 12:57pm    
I would need to find some time... the use of sorting algorithms is always not very good, compared to the case when we use a collection which is maintained sorted by value all the time, as elements are added. From OP questions, it's not obvious, but I would suggest it's not the case. Most likely, the dictionary scans through some raw data, then key is string, the value is frequency, so the first-priority search is done primarily by key. When a key is already found, the value is increased. Even this is not the most effective, because the value is a value type. If would be better to wrap frequency in class (reference type) and make this class a dictionary value; this way it could be increased "in-place".

If my assumptions are correct, there are could be two designs. One of them: same data structure, find N top (by frequency) words using no additional data. It will need scanning all the dictionary through all the elements. The sorting you suggested is very elegant, but, behind the scene, will be pretty slow, because it does excessive work. It's important to scan it only once, in just one loop by all key-value pairs. The sync of data would be another collection of the size no more than N: a new element from source dictionary is either ignored or added, and the sync collection either adds new value or add it removing old element. It needs some thinking of how to design this sync collection in an optimal way.

Another, pretty unlikely assumption is that the top N elements means exactly 5, or, some variable value but known before population of the dictionary. Formally, it can be the case, because OP says exactly 5, but, in real life, this is very unlikely; and required functionality is more generic. If this could be the case, the sync collection could be supported all the time as elements are added or removed to/from original dictionary.

I don't think this exercise really worth the effort. You are right: we are not sure what the requirements are, how important the speed is, so your solution is apparently good.
--SA
Sushil Mate 27-Oct-12 22:52pm    
You have highlighted some of the points are really informative. I don't remembered how many times I have given thought on this. while implementing I merely think about it. the approach & scenarios you have mentioned here are quite important when we deals with the speed. Thanks for your time & valuable reply. :-)
Sergey Alexandrovich Kryukov 27-Oct-12 22:53pm    
You are welcome, and thank you.
--SA
I assume you have a dictionary myDictionary as below and providing the solution :
C#
Dictionary<string,int> myDictionary = new Dictionary<string,int>();
        myDictionary.Add("X", 7);
        myDictionary.Add("Y", 5);
        myDictionary.Add("Z", 4);
        myDictionary.Add("A", 9);
        myDictionary.Add("B", 2);

        var sortedItems = (from entry in myDictionary 
                           orderby entry.Value 
                           descending select entry).ToDictionary
                           (
                            pair => pair.Key, 
                            pair => pair.Value
                           ).Take(5);


Hope this helps.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 25-Oct-12 2:02am    
Nice, elegant, but not very optimal solution, by far, because it does excessive sorting. One better solution would have a target collection of 5, replacing existing elements with "higher". I voted 4.
--SA
giri001 25-Oct-12 2:13am    
only change the query with "descending",rest is ok.
I would think of sorting the dictionary and take top 5.
However, I am not sure which object you are using. If you are using simple dictionary, I don't think, its readily possible to sort and find max.

With most probabality you can use Sorted dictionary http://msdn.microsoft.com/en-us/library/f7fta44c%28v=VS.100%29.aspx[^]

Or a SortedList object
http://msdn.microsoft.com/en-us/library/ms132319.aspx[^]

That should serve the purpose for you.

Hope that helps. If it does, mark it as answer/upvote.
Thanks
Milind
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900