How can I find the number of occurrences of a character in a string?
For example: The quick brown fox jumped over the lazy dog.
Some example
public static void main(String[] args) {
String name="AnuvratAnuvra";
char[] arr = name.toCharArray();
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for(char val:arr){
map.put(val,map.containsKey(val)?map.get(val)+1:1);
}
for (Entry<Character, Integer> entry : map.entrySet()) {
if(entry.getValue()>1){
Character key = entry.getKey();
Object value = entry.getValue();
System.out.println(key + ":"+value);
}
}
}
class A {
public static void getDuplicates(String S) {
int count = 0;
String t = "";
for (int i = 0; i < S.length() - 1; i++) {
for (int j = i + 1; j < S.length(); j++) {
if (S.charAt(i) == S.charAt(j) && !t.contains(S.charAt(j) + "")) {
t = t + S.charAt(i);
}
}
}
System.out.println(t);
}
}
class B public class B {
public static void main(String[] args){
A.getDuplicates("mymgsgkkabcdyy");
}
}
There are three ways to find duplicates
public class WAP_PrintDuplicates {
public static void main(String[] args) {
String input = "iabccdeffghhijkkkl";
findDuplicate1(input);
findDuplicate2(input);
findDuplicate3(input);
}
private static void findDuplicate3(String input) {
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
for (int i = 0; i < input.length() - 1; i++) {
int ch = input.charAt(i);
if (hm.containsKey(input.charAt(i))) {
int value = hm.get(input.charAt(i));
hm.put(input.charAt(i), value + 1);
} else {
hm.put(input.charAt(i), 1);
}
}
Set<Entry<Character, Integer>> entryObj = hm.entrySet();
for (Entry<Character, Integer> entry : entryObj) {
if (entry.getValue() > 1) {
System.out.println("Duplicate: " + entry.getKey());
}
}
}
private static void findDuplicate2(String input) {
int i = 0;
for (int j = i + 1; j < input.length(); j++, i++) {
if (input.charAt(i) == input.charAt(j)) {
System.out.println("Duplicate is: " + input.charAt(i));
}
}
}
private static void findDuplicate1(String input) {
// TODO Auto-generated method stub
for (int i = 0; i < input.length(); i++) {
for (int j = i + 1; j < input.length(); j++) {
if (input.charAt(i) == input.charAt(j)) {
System.out.println("Duplicate is: " + input.charAt(i));
}
}
}
}
}
//sample Input
/*2
7
saska
toro
winn
toro
vanco
saska
toro
3
effffdffffd
effffdffffd
effffdffffd*/
//sample output
/*4
1*/
import java.util.ArrayList;
import java.util.Scanner;
public class MyTestWhere {
/**
* @param args
*/
public static void main(String[] args) {
int count, line;
Scanner sn = new Scanner(System.in);
count = sn.nextInt();
sn.nextLine();
for (int i = 0; i < count; i++) {
line = sn.nextInt();
sn.nextLine();
// String numArr[] = new String[line];
ArrayList<String> Arr=new ArrayList<String>();
String first = sn.nextLine();
Arr.add(first);String f;
for (int j = 1; j < line; j++) {
f= sn.nextLine();
for(int k=0;k<Arr.size();k++){
if(f.equalsIgnoreCase(Arr.get(k)))break;
else if(k== (Arr.size()-1)){Arr.add(f);}
}
}
System.out.println(Arr.size());
}
}
}
I solved this with 2 dimensional array. Input: aaaabbbcdefggggh Output:a4b3cdefg4h
int[][] arr = new int[10][2];
String st = "aaaabbbcdefggggh";
char[] stArr = st.toCharArray();
int i = 0;
int j = 0;
for (int k = 0; k < stArr.length; k++) {
if (k == 0) {
arr[i][j] = stArr[k];
arr[i][j + 1] = 1;
} else {
if (arr[i][j] == stArr[k]) {
arr[i][j + 1] = arr[i][j + 1] + 1;
} else {
arr[++i][j] = stArr[k];
arr[i][j + 1] = 1;
}
}
}
System.out.print(arr.length);
String output = "";
for (int m = 0; m < arr.length; m++) {
if (arr[m][1] > 0) {
String character = Character.toString((char) arr[m][0]);
String cnt = arr[m][1] > 1 ? String.valueOf(arr[m][1]) : "";
output = output + character + cnt;
}
}
System.out.print(output);
You can also achieve it by iterating over your String
and using a switch
to check each individual character, adding a counter whenever it finds a match. Ah, maybe some code will make it clearer:
Main Application:
public static void main(String[] args) {
String test = "The quick brown fox jumped over the lazy dog.";
int countA = 0, countO = 0, countSpace = 0, countDot = 0;
for (int i = 0; i < test.length(); i++) {
switch (test.charAt(i)) {
case 'a':
case 'A': countA++; break;
case 'o':
case 'O': countO++; break;
case ' ': countSpace++; break;
case '.': countDot++; break;
}
}
System.out.printf("%s%d%n%s%d%n%s%d%n%s%d", "A: ", countA, "O: ", countO, "Space: ", countSpace, "Dot: ", countDot);
}
Output:
A: 1
O: 4
Space: 8
Dot: 1