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 757259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:20:29+00:00 2026-05-14T15:20:29+00:00

In Programming Pearls I have met the following problem. The question is this: print

  • 0

In “Programming Pearls” I have met the following problem. The question is this: “print words in order of decreasing frequency”. As I understand problem is this. Suppose there is a given string array, let’s call it s (words I have chosen randomly, it does not matter),

String s[]={"cat","cat","dog","fox","cat","fox","dog","cat","fox"};

We see that string “cat” occurs 4 times, “fox” 3 times and “dog” 2 times. So the desired result will be this:

cat
fox
dog

I have written the following code in Java:

import java.util.*;
public class string {
   public static void main(String[] args){
      String s[]={"fox","cat","cat","fox","dog","cat","fox","dog","cat"};
      Arrays.sort(s);
      int counts;
      int count[]=new int[s.length];
      for (int i=0;i<s.length-1;i++){
         counts=1;
         while (s[i].equals(s[i+1])){
            counts++;
         }
         count[i]=counts;
      }
   }
}

I have sorted the array and created a count array where I write the number of occurrences of each word in array.

My problem is that somehow the index of the integer array element and the string array element is not the same. How can I print words according to the maximum elements of the integer array?

  • 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-05-14T15:20:30+00:00Added an answer on May 14, 2026 at 3:20 pm

    To keep track of the count of each word, I would use a Map which maps a word to it’s current count.

    String s[]={"cat","cat","dog","fox","cat","fox","dog","cat","fox"};
    
    Map<String, Integer> counts = new HashMap<String, Integer>();
    for (String word : s) {
        if (!counts.containsKey(word))
            counts.put(word, 0);
        counts.put(word, counts.get(word) + 1);
    }
    

    To print the result, go through the keys in the map and get the final value.

    for (String word : counts.keySet())
        System.out.println(word + ": " + (float) counts.get(word) / s.length);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have question from programming pearls problem is following show how to use Lomuto's
This is the problem described in Programming pearls . I can not understand binary
I have this code from programming pearls #include <iostream> //#include <string> using namespace std;
i have implement this code from programming pearls and i think it should be
here is code from programming pearls this code prints random numbers in decreasing form
In Programming Pearls (second edition) Column 5, problem 5 the question is about implementing
Here is a problem from Programming Pearls 2nd edition (Chapter 8.7): Considering a real
is it just me or this code in Programming Pearls is wrong (quicksort wants
I have to implement quicksort. From Programming Pearls here is the code: public class
Page 120 of Programming Pearls 1st edition presents this algorithm for selecting M equally

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.