AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
3. Our Experts are looking for yours ?.

# corejava-programming: Program to find most occurred digit

 Digit Counter Suman works with a firm where he has to count the number of digits given to him. Manual counting of digits takes a long time. So your job is to help shyam in digitizing the counting work given to him. So if the array is 12,44,85,65,88 then your job is to print the most occurred digit and the digit that never occurred. Digits to be counted will range from 0 to 9 In the above array, most occurred digit is 8 and digits that did not occur at all are 0,3,7,9. Write a java program to achieve the above mentioned problem Input Specification line one size of array (size between 1 and 100) followed by elements of array Output Specification Print the most occurred digit and digit that never occurred in two separate lines. Sample Input: 4 125 104 44 64 Sample Output: Most occurred digit : 4 Digits that did not occur : 3,7,8,9 Note: If there are two or more digits that has occurred more, than print the greater of them. (for e.g. if 4,5 are most occurred digit than print 5) corejava x 353programming x 168 Posted On : 2017-11-27 16:52:36.0 Divesh 101180 1

 import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class DigitCounter { public static Character[] possibleDigits={'0','1','2','3','4','5','6','7','8','9'}; public static void main(String[] args) { Scanner input=new Scanner(System.in); int n=input.nextInt(); Integer number=0; int temp =0; int max=0; char mostOccuredDigit= ´ ´; Map map=new HashMap(); for(int i=0;imostOccuredDigit){ mostOccuredDigit=digit; } } } System.out.println(""); System.out.println("Most occurred digit : "+mostOccuredDigit); List notOccuredList=new ArrayList(Arrays.asList(possibleDigits)); notOccuredList.removeAll(map.keySet()); System.out.println("Digits that did not occur : "+notOccuredList.toString().replace("[", "").replace("]", "")); } } Posted On : 2017-12-03 16:56:22 Satisfied : 0 Yes  0 No Rishi Kumar 523188222642Reply This Thread 0
thanks you sir for your answer..but can you give me code without using hasmap and map..
Divesh
101  18  0
Posted On :2017-12-04 17:12:13.0
Hi Divesh,
Sure, We can solve it without using Map. Please refer below answer. We can assume an array of size 10 , and its index from 0-9 represents the possible digits. and just update the respective index by +1 on occurrence of that digit.
Rishi Kumar
523  1882  22642
Posted On :2017-12-04 22:47:52.0

 import java.util.Scanner; public class DigitCounter { public static void main(String[] args) { Scanner input=new Scanner(System.in); int n=input.nextInt(); Integer number=0; int temp =0; int max=0; char mostOccuredDigit= ' '; int[] digits=new int[10];//assume array index represents 0-9 digits for(int i=0;imostOccuredDigit){ mostOccuredDigit=digit; } } } System.out.println(""); System.out.println("Most occurred digit : "+mostOccuredDigit); System.out.print("Digits that did not occur : "); boolean isFirst=true; for(int j=0;j