Tell me, please, how can you implement a method that returns an object that appears in the list most often.

For example, I have different colors in my list: Color.black, Color.white, Color.blue .... And I have to determine that the color Color.randcolor occurs most often.

But how to do that...

    5 answers 5

    Something came to mind was a hemorrhoid, but still the idea: create a map, a key is a color, a value is the number of elements in the list of this color. Then just go through the list and update the necessary values. Further sorting and answer.

    • Only to search for the maximum sorting is not needed. One pass is enough. - avp
    • it is possible that way) I just didn’t sleep all night, so I suggested the first thing that looked into the skull) - Stas0n
    • MAP is probably the easiest solution - wwvv

    The "core" of the list item can be something like this. In this case, with each access to the color, the time when access was saved. Of course, you can more cleverly determine what “frequent” access is and save, say, the number of hits in the last 24 hours / hour and so on.

    Further, in general, it is no longer a matter of writing a method in the MyList class that will produce "the most frequently used color".

    class ListElement { private long lastUsed; private Color color; public Color getColor() { lastUsed=System.getCurrentMillis(); return color; } public long getLastUsed() { return lastUsed; } } class MyList { private ArrayList<ListElement> myList; //... } 
    • why time?)) need group by))) - Gorets

    It is solved using the lambdaj library groupings :

     List<Item> items = Arrays.asList(new Item("1"), new Item("2"), new Item("3")); Group<Item> group = selectMax(group(items, by(on(Item.class).getValue())).subgroups(), on(Group.class).getSize()); 

    PS The code did not run, but it should work.

      There is such a thing as LRU cache, you can read about it everywhere :), so if you take LinkedHashMap for example and store your colors there, then the top element will be the one that was most used, which is common and solves your problem.

      upd

      Without bicycles, it is better to use a collection, store a pair of key - color, value - number of uses, wrap addition to a method that, in the case of a copy of a key - increments the value, that is, the method as above, there are simply collections that do not store duplicate keys - for this, do not need extra checks.

      • LRU == Least Recently Used - Barmaley
      • With LinkedHashMap number will not work, because the order of the elements does not change when the element is re-inserted. - a_gura
      • @a_gura, google, will pass. In this question, the number of repetitions in the list is necessary, I do not understand this approach, why put copies ?, IMHO, this is more like a cache that can fit. - Gorets
      • If the key is re-inserted into the map. docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html And the question is about the most common element in the list. - a_gura

      http://ru.wikipedia.org/wiki/MapReduce ) but of course if you plan to use a distributed network for counting))