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

  • Home
  • SEARCH
  • 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 8639999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:10:06+00:00 2026-06-12T11:10:06+00:00

The method in question is below. This method is supposed to take a string

  • 0

The method in question is below. This method is supposed to take a string and return the frequency of an item using bag adt. I have it working, however if I (for example) enter “test”, it will show the outputs T, S, and T with their respective(and albeit, correct) frequencies. However, I’d like the output to just be T, S with their frequencies.

    public int getFrequency(String str){
        int index=0;
        char[] nArray = new char[sArray.length];

        for(int i=0;i<sArray.length; i++){
            char a = sArray[i];
            String s = Character.toString(a);
            index = consonants.getFrequencyOf(s);
            if(index != 0 && consonants.contains(s)==true){
                for(int x=0;x<nArray.length;x++){
                    if(nArray[i] == sArray[x]){
                        continue;
                    }
                    else{
                        System.out.print(s + ": ");
                        System.out.println(index);
                        nArray[i] = sArray[i];
                        break;
                    }       
                }
            }
        }
        return index;
    }
  • 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-12T11:10:07+00:00Added an answer on June 12, 2026 at 11:10 am

    You should rather use a HashMap, to store the Characters with their correspondin frequency: –

    public Map<Character, Integer> getFrequency(String str){
    
        String vowels = "aeiouAEIOU";
        Map<Character, Integer> freqMap = new HashMap<Character, Integer>();
    
        for(int i=0;i<str.length(); i++) {
    
            char ch = str.charAt(i);
    
            // If character is a not consonant.. continue with next iteration
    
            if (vowels.contains(ch)) {
                continue;
            }
    
            Integer val = freqMap.get(ch);
    
            if (val != null) {
                // Put new entry in Map.. With character and count = 1
    
            } else {
                // Increment val by 1, and update the map for this character
            }
    
        }
        return freqMap;
    }
    

    Just a confirmation about your code: –

    index = consonants.getFrequencyOf(s);
    if(index != 0 && consonants.contains(s)==true){
    

    The 2nd condition looks Vague to me.. Because if index !=0, then it means that consonants contains s.. Then why check again??

    Or, your getFrequencyOf(s) is doing something else than checking the containment, that we can’t see??

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to capture audio, using the method in this question ; with AVCaptureSession
Newbee question here - I have this method in the view controller m: -(void)
Drupal Question: Does anyone have a method of using Views and highlighting a selected/current
I am trying to get this method in a String Filter working: public function
This question is about overriding a virtual method in a derived class with a
Fairly simple question: I have an init method on my class that has the
This question got me thinking about the max_size method in vector class. It is
NOTE: I have updated this since originally asking the question to reflect some of
In general , let's say you have a method like the below. def intersect_two_lists(self,
TDD is supposed to have 100% code coverage. Does this mean one is supposed

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.