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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:38:53+00:00 2026-06-10T10:38:53+00:00

So I was developing an algorithm to count the number of repetitions of each

  • 0

So I was developing an algorithm to count the number of repetitions of each character in a given word. I am using a HashMap and I add each unique character to the HashMap as the key and the value is the number of repetitions. I would like to know what the run time of my solution is and if there is a more efficient way to solve the problem.

Here is the code :

public static void getCount(String name){
        public HashMap<String, Integer> names = new HashMap<String, Integer>() ;
        for(int i =0; i<name.length(); i++){

            if(names.containsKey(name.substring(i, i+1))){
                names.put(name.substring(i, i+1), names.get(name.substring(i, i+1)) +1);
            }
            else{
                names.put(name.substring(i, i+1), 1);
            }
        }

        Set<String> a = names.keySet();
        Iterator i = a.iterator();

        while(i.hasNext()){
            String t = (String) i.next();
            System.out.println(t + " Ocurred " + names.get(t) + " times");
        }
    }
  • 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-10T10:38:55+00:00Added an answer on June 10, 2026 at 10:38 am

    The algorithm has a time complexity of O(n), but I’d change some parts of your implementation, namely:

    • Using a single get() instead of containsKey() + get();
    • Using charAt() instead of substring() which will create a new String object;
    • Using a Map<Character, Integer> instead of Map<String, Integer> since you only care about a single character, not the entire String:

    In other words:

    public static void getCount(String name) {
        Map<Character, Integer> names = new HashMap<Character, Integer>();
        for(int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            Integer count = names.get(c);
            if (count == null) {
                count = 0;
            }
            names.put(c, count + 1);
        }
        Set<Character> a = names.keySet();
        for (Character t : a) {
            System.out.println(t + " Ocurred " + names.get(t) + " times");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm developing an implementation of ant colony algorithm, and stuck at find nearest value
I am working on developing a lossless compression algorithm using MATLAB. I would like
I'm developing an algorithm to parse a number out of a series of short-ish
I am developing an algorithm to reorder packets in a transmission. Each packet has
I'm developing a parallel algorithm on a Intel i5-core machine, which has two cores,
Just for kind of fun I'm developing almost every algorithm (if possible) shown in
While developing an application using gwt in ecliplse crashed. Now the server is running
I'm having some trouble developing an algorithm to determine the minimum of a list
I was wondering what your ideas were in developing an efficient algorithm for doing
I'm developing a little programme to implement some sorts algorithm in C++. I've decided

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.