Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,086 questions

55,960 answers

573 users

How to find the frequency of each digit (0–9) in a number with Rust

1 Answer

0 votes
fn count_digits<N: ToString>(n: N) -> [usize; 10] {
    let mut freq = [0usize; 10];

    for ch in n.to_string().chars() {
        let digit = ch.to_digit(10).unwrap() as usize;
        freq[digit] += 1;
    }

    freq
}

fn main() {
    let number = 120220340501u64;

    let freq = count_digits(number);

    for (digit, count) in freq.iter().enumerate() {
        println!("{digit}: {count}");
    }
}


/*
run:

0: 4
1: 2
2: 3
3: 1
4: 1
5: 1
6: 0
7: 0
8: 0
9: 0

*/

 



answered Jul 2 by avibootz
...