Tuesday, 28 January 2014

Given a number N, now find the number of occurrences of each digit

Given a number N, now find the number of occurrences of each digit 0..9 from 0 to N
Ex:
i/p: 12
o/p:
2
5
2
1
1
1
1
1
1
1

Implementation:

class GivenNumberN_FindTheNumberOfOccurrencesOfEachDigit{
static int[] findOccurrences(int n){
if(n == 0)  //Base condition
return null;

if(n < 0)  //If number is negitive then make it positive.
n = n * (-1);

int []arr = new int[10];  //array to hold the occurrences of numbers
init(arr);  //initialize the array with zero
 
         arr[0]++;
for(int i = 1; i <= n; i++){  //loop till given number to find the occurrence
int temp = i;
while( temp > 0){    //find the occurrence of digit in current number - temp
int rem = temp % 10;  //get the last digit of current number
arr[rem]++;  //increment the count of digit
temp /= 10;
}
}
return arr;  //return the array of occurrence of number
}

static void init(int []arr){
for(int i = 0; i < 10; i++)
arr[i] = 0;
}

public static void main(String... args){
int n = 12;
int []count = findOccurrences(n);
System.out.println(java.util.Arrays.toString(count));
}
}

6 comments:

  1. Here is the code with order of O(n)


    package coding;

    import java.util.Scanner;

    public class CountingDigits {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String str="100";
    int n1,n2,n3;
    n1=Integer.parseInt(str)/100;
    n2=(Integer.parseInt(str)%100)/10;
    n3=(Integer.parseInt(str)%100)%10;
    System.out.println("n1="+n1+" n2="+n2+" n3="+n3);
    int count[]={0,0,0,0,0,0,0,0,0,0};
    for(int j=0;j<10;){

    if(j==n1){
    count[j]++;
    }
    if (j==n2)
    count[j]++;
    if(j==n3)
    count[j]++;
    j++;
    }
    for(int i=0;i<10;i++)
    System.out.println("The count of"+i +" is "+count[i]);

    }

    }

    ReplyDelete
  2. public static void main(String[] args) {
    // TODO Auto-generated method stub

    int[] result=new int[10];
    result=GivenNumberN_FindTheNumberOfOccurrencesOfEachDigit(-4532);
    for(int i=0;i0){
    int remainder= temp%10;
    tempArray[remainder]++;
    temp=temp/10;
    }
    return tempArray;

    }

    public static void init(int []arr){
    for(int i = 0; i < 10; i++)
    arr[i] = 0;
    }

    ReplyDelete
  3. #include

    int main()
    {
    int a[10];
    for(int i=0; i<10; i++)
    {
    a[i] = 0;
    }
    a[0] = 1;
    for(int i=1; i 0 )
    {
    a[ j%10 ]++;
    j /= 10;
    }
    }
    for(int i=0; i<10; i++)
    {
    std::cout << a[i] << std::endl;
    }

    ReplyDelete
  4. package test;

    public class prob1 {

    int arr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    void func() {
    int t, j, x;
    for (int i = 0; i <= 12; i++) {

    t = i % 10;
    arr[t]++;
    if ((i / 10) > 0) {
    x = i / 10;
    while (x > 0) {

    t = x % 10;
    arr[t]++;
    x = x / 10;
    }

    }
    }
    }

    void print() {
    for (int i = 0; i < 10; i++)
    System.out.println(arr[i] + "\n");
    }

    public static void main(String args[]) {

    prob1 obj = new prob1();
    obj.func();
    obj.print();

    }

    }

    ReplyDelete
  5. package test;

    public class prob1 {

    int arr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    void func() {
    int t, j, x;
    for (int i = 0; i <= 12; i++) {

    t = i % 10;
    arr[t]++;
    if ((i / 10) > 0) {
    x = i / 10;
    while (x > 0) {

    t = x % 10;
    arr[t]++;
    x = x / 10;
    }

    }
    }
    }

    void print() {
    for (int i = 0; i < 10; i++)
    System.out.println(arr[i] + "\n");
    }

    public static void main(String args[]) {

    prob1 obj = new prob1();
    obj.func();
    obj.print();

    }

    }

    ReplyDelete
  6. import java.util.Hashtable;


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

    int number = 326631232;
    split(number);
    }

    private static void split(int number) {
    Hashtable ht = new Hashtable();

    while(number > 0){
    int digit = number % 10;
    //System.out.println(digit);

    if(ht.containsKey(digit)){
    int currentValue = ht.get(digit);
    ht.put(digit, currentValue + 1);
    }
    else{
    ht.put(digit, 1);
    }

    number /= 10;
    }

    for(int i = 0; i <= 9; i++){
    int freq = 0;
    if(ht.containsKey(i)){
    freq = ht.get(i);
    }
    System.out.println(i + " occurred: " + freq + " time(s).");
    }
    }
    }

    ReplyDelete