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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:34:37+00:00 2026-06-04T02:34:37+00:00

I wanted to get the average of each unique character in a string. I

  • 0

I wanted to get the average of each unique character in a string. I have an example here as it would be easier to illustrate it.

String:

The big brown fox

Average of each character including spaces:

T = 1/17 = .058
h = 1/17 = .058
e = 1/17 = .058
' '= 3/17 = .176
b = 2/17 = .117
i = 1/17 = .058
g = 1/17 = .058
r = 1/17 = .058
o = 2/17 = .117
w = 1/17 = .058
n = 1/17 = .058
f = 1/17 = .058
x = 1/17 = .058

So far all my attempts have failed, think my brain is not working at the moment. How do I code this? Any help or input will be greatly appreciated.

I have this code as a solution. It just came to me when I was copy pasting my code here in stack. I hope this is not a re-post because I just answered the same thing a few minutes ago but it didn’t show up.

 Map<String, Integer> storeCharCount = new HashMap<String, Integer>();

        String a = "The big brown fox";


        for (int x=0; x<a.length(); x++){
            char getChar = a.charAt(x);
            String convGetChar = Character.toString(getChar);

            Integer countChar = storeCharCount.get(convGetChar);
            storeCharCount.put(convGetChar, (countChar==null?countChar=1:countChar+1));

        }
        System.out.println("Map: "+ storeCharCount);
        double RelFrequency = 0;
        for (Map.Entry<String, Integer> getValue: storeCharCount.entrySet()){

            RelFrequency = (double)(getValue.getValue())/(a.length());
            System.out.println("Character "+getValue.getKey() +"  Relative Frequency: "+RelFrequency);

        }

Here is the output

Map: {f=1, g=1,  =3, e=1, b=2, n=1, o=2, h=1, i=1, w=1, T=1, r=1, x=1}
Character f  Relative Frequency: 0.058823529411764705
Character g  Relative Frequency: 0.058823529411764705
Character    Relative Frequency: 0.17647058823529413
Character e  Relative Frequency: 0.058823529411764705
Character b  Relative Frequency: 0.11764705882352941
Character n  Relative Frequency: 0.058823529411764705
Character o  Relative Frequency: 0.11764705882352941
Character h  Relative Frequency: 0.058823529411764705
Character i  Relative Frequency: 0.058823529411764705
Character w  Relative Frequency: 0.058823529411764705
Character T  Relative Frequency: 0.058823529411764705
Character r  Relative Frequency: 0.058823529411764705
Character x  Relative Frequency: 0.058823529411764705   
  • 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-04T02:34:39+00:00Added an answer on June 4, 2026 at 2:34 am

    Here’s my suggestion. Start by iterating over every character in the String, update freq if found else add 1. Then iterate again to print result :

            String s = "The big brown fox";
        Map<Character, Float> m = new TreeMap<Character, Float>();
        for (char c : s.toCharArray()) {
            if (m.containsKey(c))
                m.put(c, m.get(c) + 1);
            else
                m.put(c, 1f);
        }
    
        for (char c : s.toCharArray()) {
            float freq = m.get(c) / s.length();
            System.out.println(c + " " + freq);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get the average for certain columns for each row. I
I wanted to get the words from a cell. For example, cell A2 has
When I wanted to get Android source code, I knew that I have to
I have a DropDownList inside in ListView.. I wanted to get a data when
I wanted to get the value of a Numeric cell as a simple string.
I wanted to get a sense of the average user experience of having to
i have a login page and wanted to get authenticated using web server and
I wanted to get ideas from the SO community about this issue. Here is
I wanted to get some advice, I have started on a new project to
I wanted to get image url "http://www.test.com/image.jpg" out from the string: "&lt;img align=&quot;right&quot; alt=&quot;Title

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.