-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOccur.java
More file actions
42 lines (31 loc) · 1000 Bytes
/
Copy pathOccur.java
File metadata and controls
42 lines (31 loc) · 1000 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
34
35
36
37
38
39
40
41
42
package boot;
import java.util.Scanner;
public class Occur {
public static void main(String[] args) {
/*Challenge 2:
Write a solution to find the character that has the highest number of occurrences within a certain string, ignoring
case. If there is more than one character with equal highest occurrences, return the character that appeared first
within the string.*/
String str, str1;
char hichar = ' ';
int i, max = -1;
int[] count = new int[256];
Scanner sc= new Scanner(System.in);
System.out.print("\nEnter String:\n ");
str1 = sc.nextLine();
str=str1.toLowerCase();
for(i = 0; i < str.length(); i++)
{
count[str.charAt(i)]++;
}
for(i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if(max < count[ch]) {
max = count[ch];
hichar = ch;
}
}
System.out.println("\n Highest Occurrance character is: " + hichar);
}
}