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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:32:17+00:00 2026-05-18T09:32:17+00:00

The code below will count the occurences of every character. If i have abc

  • 0

The code below will count the occurences of every character. If i have abc in the text file output would be a 1 b 1 c 1. I read in many sites that for loop will be taking much of time and it is better to implement the same using hash map. Can any of you help me how to convert this program implementing hash map?

 import java.io.*;

    class Count_Char {
    public static void main(String[] args) {
        try
        {
    FileInputStream file = new FileInputStream("D:\\trial.txt");
    DataInputStream dis = new DataInputStream(file);
    BufferedReader br = new BufferedReader(new InputStreamReader(dis));
    String Contents="";
    String str="";
    while ((Contents = br.readLine()) != null) {
    str+=Contents;
    }
    char[]char_array =str.toCharArray();
    for(int count =0;count<char_array.length;count++){
    char ch= char_array[count];
    int counter=0;
    for ( int i=0; i<char_array.length; i++){
    if (ch==char_array[i])
    counter++;
    }
    boolean flag=false;
    int j=count-1;
    while(j>=0)
        {

        if(ch==char_array[j])
            flag=true;
            j--;
        }
    if(!flag){
    System.out.println(ch+" "+counter);
    }
    }
        }catch(IOException e1){
            System.out.println(e1);
        }
        }
    }
  • 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-18T09:32:18+00:00Added an answer on May 18, 2026 at 9:32 am

    Quick pseudo code. Basically the trick here is that you save the characters as keys in the Map and the value being the count of the occurrences for that character (key/value pair).

     //declare a map to hold your characters and their counters
     Map<String,Integer> charCounter = new HashMap<String,Integer>();
     //the following if else logic goes when you are looping through your tokens
        if(charCounter.containsKey(<your character>)){
               charCounter.put(<your character>,charCounter.get(<your character>)+1);
        }else{
              charCounter.put(<your character>,1);
        }
    

    After you are done traversing, you can print the map this way.

    for(String key : charCounter.keySet()) {
                System.out.println(key+" "+charCounter.get(key));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Developer to debug code like below, the output result will
I have a chart (code to replicate will be below) that has two lines
I have a mysqli code below where it uploads a file and insert data
This code below basically will count the number of occurrences of a word inside
The code below will validate required fields within currently visible fieldsets, but only if
The code below will report Syntax error message: type 'a edge = |Empty |End
I will post my code below as a quick note I define both of
The code below is working. It will progress through all columns in the sheet
in the code below at first if statements block (there will be more than
My app is almost complete. In the second snippet of code below, you will

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.