Count the Number of Times Each Number Occurs

Count the Number of Times Each Number Occurs

Solution

package com.assignment;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// write your code here

int [] arr = new int[1000];     // can take upto 1000 inputs

int [] count  = new int[101];       // can take integers from 1 to 100

int i, temp =0;

System.out.println(“Enter the integers between 1 and 100: “);

Scanner s = new Scanner(System.in);

for(i=0; ; i++){

arr[i] = s.nextInt();

if(arr[i]==0)

break;

}

for(i = 0; i<arr.length; i++)

{

temp = arr[i];

count[temp]++;

}

for(i =1 ; i<count.length; i++){

if(count[i]>0 && count[i]==1)

System.out.println(i + ” occurs ” + count[i] + ” time”);

else if(count[i]>=2)

System.out.println(i + ” occurs ” + count[i] + ” times”);

}

}

}