Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8245321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:08:25+00:00 2026-06-07T22:08:25+00:00

I am looking at a redundant list of Strings, e.g. { One, One, One,

  • 0

I am looking at a redundant list of Strings, e.g.

{ "One", "One", "One", "Two", "Three", "Three" }

What is the best way to count the occurrences, then create a non-redundant list of the strings, sorted by the number of occurrences?

The result I want is a List like this:

{ "One", "Three", "Two" }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:08:26+00:00Added an answer on June 7, 2026 at 10:08 pm

    You can use the trick in the most voted answer of this question about how to sort the map by its values.

    Here is an example implementation (I have added generics to the comparator):

    • you add the strings / occurences to a hashmap
    • put everything in a TreeMap with a custom comparator that sorts on the values
    • put the keys back in a list
    public static void main(String[] args) {
        String[] strings = {"One", "One", "One", "Two", "Three", "Three"};
    
        //Count occurences
        Map<String, Integer> map = new HashMap<String, Integer>();
    
        for (String s : strings) {
            if (map.containsKey(s)) {
                map.put(s, map.get(s) + 1);
            } else {
                map.put(s, 1);
            }
        }
    
        ValueComparator<String, Integer> comparator = new ValueComparator<String, Integer> (map);
        Map<String, Integer> sortedMap = new TreeMap<String, Integer> (comparator);
        sortedMap.putAll(map);
    
        List<String> sortedList = new ArrayList<String> (sortedMap.keySet());
    
        System.out.println(sortedMap);
        System.out.println(sortedList);
    
    }
    
    static class ValueComparator<K, V extends Comparable<V>> implements Comparator<K> {
    
        Map<K, V> map;
    
        public ValueComparator(Map<K, V> base) {
            this.map = base;
        }
    
        @Override
        public int compare(K o1, K o2) {
             return map.get(o2).compareTo(map.get(o1));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for a way to get path of file (and then process it in
I am looking for the best way to model a scenario in C#, which
Looking for a perl one-liner what will find all words with the next pattern:
Looking for best advice on how to do this: I have an insert like
Looking for a control that allows to select one text value at a time
Looking for a simple bit of JS to count the number of items in
Coming from a non-OO background in Matlab, I'm looking to perform some of the
I'm looking to clean up my controller, as it seems heavy and redundant. Any
Given an ugly and probably long, inefficient, redundant and complicated looking query, is there
I'm looking for the quickest/shortest way to get the first value from comma separated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.