Wyliczanie ilości konkretnej cyfry w danym ciągu liczb

0

Wyliczanie ilości powtórzeń konkretnej cyfry w danym ciągu liczb

Mam tablicę zawierającą liczby [11,12,13,14,15,(..),111,112,113,(...),711(...)] <= 100000

W jaki sposób mogę wyliczyć ile razy powtarza się np cyfra '1'? w danej tablicy, np 11, zawiera dwie jedynki, 711, 111 etc.
Czy wrzucanie tego do tablicy ma jakiś sens, i potem wyszukanie wartości jedynek?

Do tej pory udało mi się skleić taki kod, jednak nie do końca on działa

import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

public class Solutions {


    public static void main(String[] args) throws IOException {


        Scanner input = new Scanner(System.in);
        int number = input.nextInt();
        int[] anArray = IntStream.rangeClosed(1, number).toArray();
        String anArrayToString = Arrays.toString(anArray);
        Pattern pattern = Pattern.compile("[1]{1,}");
        Matcher matcher = pattern.matcher(anArrayToString);
        if(matcher.find())
        {
            System.out.println("Found: " + matcher.group(1));
        }
        else
        {
            System.out.println("No match");
        }
    }

    }

1

Najprosciej to chyba strumieniami:

IntStream.of(ints)
.mapToObj(Integer::toString)
.reduce("",(accumulator, element)->accumulator+element)
.chars()
.filter(ch -> ch == '1')
.count()

Ale nie odpowiadam za optymalność tego rozwiązania xD

1

O to chodzi?

int values[] = {10, 20, 50, 43, 34, 234, 6, 65, 34, 10, 20, 50, 6};
        int index = 0, currentValue, count;
        Arrays.sort(values);

        while(index < values.length) {
            currentValue = values[index];
            count = 0;

            for(; index < values.length && currentValue == values[index];
                    index++, count++ );

            System.out.println(currentValue + " wystepuje: " + count + " razy.");
        }
0

Dziękuję za pomoc, rozwiązałem zadanie, wrzucam wynik może komuś się przyda, krytyka mile widziana

import java.util.Scanner;
import java.util.stream.IntStream;

public class Solutions {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int number = input.nextInt();
        int[] arr = IntStream.rangeClosed(1, number).toArray();
        int sum = IntStream.of(arr)
                .map(Solutions::countOnes)
                .sum();
        System.out.println(sum);
    }

    private static int countOnes(int number) {
        int result = 0;
        while (number != 0) {
            int a = number % 10;
            number /= 10;
            if (a == 1) {
                result++;
            }
        }
        return result;
    }
}
2

Ta wersja ostatnia wygląda nieźle, ale jest to rozwiązanie brute-force.
W książce "cracking the coding interview" - zadanie 18.4 i 2.4 masz jeszcze opisane rozwiązanie chyba bardziej optymalne.
Poniżej jedno z możliwych rozwiązań:
https://fredfsh.com/2013/04/24/%e7%bc%96%e7%a8%8b%e4%b9%8b%e7%be%8e-2-4-1%e7%9a%84%e6%95%b0%e7%9b%ae/

1 użytkowników online, w tym zalogowanych: 0, gości: 1