Digit Frequency Histogram - Problem

Given a positive integer, count the frequency of each digit (0-9) and display a horizontal histogram using '*' characters.

For each digit that appears in the number, print the digit followed by a colon, then print '*' characters equal to the frequency of that digit.

Rules:

  • Only display digits that actually appear in the number
  • Display digits in ascending order (0, 1, 2, ..., 9)
  • Each histogram bar should be on a separate line
  • Format: digit: **** (digit, colon, space, then stars)

Input & Output

Example 1 — Basic Case
$ Input: number = 1223
Output: 1: * 2: ** 3: *
💡 Note: Digit 1 appears 1 time, digit 2 appears 2 times, digit 3 appears 1 time. Display in ascending order with histogram bars.
Example 2 — Repeated Digits
$ Input: number = 112233
Output: 1: ** 2: ** 3: **
💡 Note: Each digit 1, 2, and 3 appears exactly 2 times, so each gets a 2-star histogram bar.
Example 3 — Single Digit
$ Input: number = 7
Output: 7: *
💡 Note: Only digit 7 appears once, so output shows just '7: *'

Constraints

  • 1 ≤ number ≤ 1018
  • Number is always positive

Visualization

Tap to expand
INPUTALGORITHMRESULT1223Number: 12231Count each digit2Digit 1: appears 1 time3Digit 2: appears 2 times4Digit 3: appears 1 timeStore counts in frequency arrayHistogram Output:1: *2: **3: *Visual frequency displayKey Insight:Count digit frequencies in one pass, then generate histogram bars using repeated asterisksTutorialsPoint - Digit Frequency Histogram | Frequency Counting Approach
Asked in
Amazon 15 Google 12
8.5K Views
Medium Frequency
~15 min Avg. Time
340 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen