Zoho

## Interview Question

Applications Developer Interview

-

# Write a Java program to count the number of occurances of each number in a series of numbers.

Tags:java, programming

3

Devanand on

0

Anonymous on

0

import java.util.Scanner; public class Occurrence { public static void main(String[] args) { int n, count = 0, i = 0; Scanner s = new Scanner(System.in); System.out.print("Enter no. of elements you want in array:"); n = s.nextInt(); int a[] = new int[n]; int x[] = new int[n]; System.out.println("Enter all the elements:"); for(i = 0; i < n; i++) { a[i] = s.nextInt(); } System.out.print("Enter the element of which you want to count number of occurrences:"); for(i = 0; i < n; i++) { x[i] = a[i]; } for(i = 0; i < n; i++){ count = 0; for(int j = 0; j < n; j++) { if(a[j] == x[i]) { count++; } } System.out.println("Number of Occurrence of the Element:"+a[i]+"is"+count); } } }

Java program to count the number of occurences on

0

import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Test { public static void main(String[] args){ Map map = new HashMap(); Scanner s1 = new Scanner(System.in); int a[] = new int[10]; System.out.println("Please enter the 10 numbers"); for(int i=0;i<10;i++){ a[i]=s1.nextInt(); } for(int j=0;j<10;j++){ int count = 1; for(int k=0;k<10;k++){ if(j!=k){ if(a[j]==a[k]){ count++; } } } map.put(a[j],count); } for(Map.Entry m:map.entrySet()){ System.out.println("The value "+m.getKey()+" occurs "+m.getValue()+" times in the series which u have entered."); } } }

Suhail Rasith R on

0