Skip to content

Automation Testing

Java, Selenium etc..

  • Java
  • Selenium
  • Interview Question
  • Core Java Coding
  • Rest APIS
  • Manual Testing
  • SQL Queries
  • Postman
  • Toggle search form

String Char Counts Using Collections

Posted on May 10, 2023May 10, 2023 By admin
String Char Counts Using Collections: 
package InterViewQuestions;

import java.util.HashMap;
import java.util.Map;

public class StringCharCountsUsingCollections {
//string = hemchandrraBhatt
//print number chars repeated
public static void main(String[] args) {
String str = "hemchandrabhatt";
HashMap<Character, Integer> mapCharCount = new HashMap<>();
char[] charArr = str.toCharArray();
for (char c : charArr) {
if (mapCharCount.containsKey(c)) {
mapCharCount.put(c, mapCharCount.get(c) + 1);
} else {
mapCharCount.put(c, 1);
}
}
//System.out.println(mapCharCount);
for (Map.Entry entry : mapCharCount.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}

// Time complexity: O(n) where n is length of given string
//Auxiliary Space: O(n)

}
}
Core Java Coding Questions Tags:Java program to count the occurrence of each character in a string using Hashmap, String Char Counts Using Collections, StringCharCountsUsingCollections

Post navigation

Previous Post: GUI Testing for Payment Gateway
Next Post: Create a Java program to store the Name and Salary of employees

Recent Posts

  • Can you write java program for prime number 1to 50 and skip printing 30 and 40 numbers
  • Java 8 features
  • Imp Links
  • Create a Java program to store the Name and Salary of employees
  • String Char Counts Using Collections

Recent Comments

No comments to show.

Copyright © 2022 okhalaTech