-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathHIgestOccuranceCount.java
More file actions
33 lines (28 loc) · 927 Bytes
/
HIgestOccuranceCount.java
File metadata and controls
33 lines (28 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Arrays;
import java.util.Scanner;
public class HIgestOccuranceCount {
public static void main(String[] args) {
int highestCount = 0;
char highestChar ='#';
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the String");
String str =scanner.next(); //tyrthgfhtttvfgfhtvfdgtt
char array[] = str.toCharArray();
Arrays.sort(array);
str = new String(array);
System.out.println("After Sort "+str);
for(int i = 0;i<str.length();i++){
char firstChar = str.charAt(i);
int firstIndex = str.indexOf(firstChar);
int lastIndex = str.lastIndexOf(firstChar);
int countOccurance = (lastIndex - firstIndex)+1;
//System.out.println("Char is "+firstChar+" "+countOccurance);
if(countOccurance>highestCount){
highestCount = countOccurance;
highestChar=firstChar;
}
i = lastIndex;
}
System.out.println("Char is "+highestChar+" "+highestCount);
}
}