AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
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 333
programming x 150
Posted On : 2017-11-27 16:52:36.0
profile Divesh - anyforum.in Divesh
8800
up-rate
2
down-rate

Answers


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<Character,Integer> map=new HashMap<Character, Integer>();
for(int i=0;i<n;i++){
number = input.nextInt();
for(char digit:number.toString().toCharArray()){
if (map.containsKey(digit))
map.put(digit, map.get(digit)+1);
else
map.put(digit, 1);
temp=map.get(digit);
if(max<temp){
max=temp;
mostOccuredDigit=digit;
}else if(max==temp && digit>mostOccuredDigit){
mostOccuredDigit=digit;
}
}
}
System.out.println("");
System.out.println("Most occurred digit : "+mostOccuredDigit);
List<Character> notOccuredList=new ArrayList<Character>(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
profile Rishi Kumar - anyforum.in Rishi Kumar
523185418866
Reply This Thread
up-rate
0
down-rate
Comments
thanks you sir for your answer..but can you give me code without using hasmap and map..
profile Divesh - anyforum.in Divesh
88  0  0
Posted On :2017-12-04 17:12:13.0
Leave a Comment
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.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1854  18866
Posted On :2017-12-04 22:47:52.0
Leave a Comment

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;i<n;i++){
number = input.nextInt();
for(char digit:number.toString().toCharArray()){
int digitNumber=digit-'0';
digits[digitNumber]=digits[digitNumber]+1;
temp=digits[digitNumber];
if(max<temp){
max=temp;
mostOccuredDigit=digit;
}else if(max==temp && digit>mostOccuredDigit){
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<digits.length;j++){
int count= digits[j];
if(count==0){
System.out.print((!isFirst?",":"")+j);
isFirst=false;
}
}
}
}

Posted On : 2017-12-04 22:44:54
Satisfied : 0 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185418866
Reply This Thread
up-rate
0
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in