Learn how to use a HashMap to find duplicate characters in a string — a popular coding interview question and a great hands-on exercise for QA automation testers. Understand how frequency counting works using Java’s HashMap and how to filter duplicates cleanly.
🔍 What This Program Does (In Simple Words):
Takes a string (e.g., "Automation Testing").
Breaks it down character by character.
Uses a HashMap to count how many times each character appears.
Then prints only those characters that appear more than once.
📊 Step-by-Step Explanation:
🟦 Step 1: Input String
The input is a regular string: "Automation Testing".
🟩 Step 2: Count Characters
A map is used to track each character in the string.
It works like this:
If a character has already appeared, increase its count.
If not, add it to the map with a count of 1.
🟨 Step 3: Loop Through the Map
After counting, we go through the map:
If a character appears more than once, it's a duplicate.
Those characters and their counts are printed.
🧪 Example Output (Case-Sensitive):
Let’s take the input string "Automation Testing".
Total characters: 18
Duplicates printed might be:
t → 3 times
o → 2 times
n → 2 times
(Note: Output is case-sensitive and includes spaces if they are duplicates too.)
🌳 Flowchart-Like Breakdown
Input → "Automation Testing"
|
v
Break into characters
|
v
Count frequency using HashMap
|
v
Filter entries with count -- 1
|
v
Print each duplicate character and its frequency
🧠 Key Java Concepts Behind the Scenes
1️⃣ HashMap for Frequency Counting
Stores characters as keys and their frequency as values.
Perfect for quickly checking duplicates.
2️⃣ toCharArray()
Converts the string into an array of characters.
Makes it easy to loop through each letter.
3️⃣ getOrDefault()
Handy method to avoid null checks.
Automatically adds 1 to the current count or defaults to 0.
4️⃣ Entry Filtering
We print only the characters where the value is greater than 1.
⚠️ Case Sensitivity Note:
By default, the logic distinguishes uppercase and lowercase characters.
Example:
'A' and 'a' are treated as different characters.
✅ Want to make it case-insensitive?
Convert the input to lowercase before processing:
"Automation Testing".toLowerCase()
📌 Real-World QA Use Case:
You can apply this same HashMap logic in automation to:
Check if input fields accept duplicate characters.
Validate random string generators.
Detect repeated values in API responses or CSV test data.
🎯 Interview Tip for QA Coders:
This is a classic Java coding interview question. It tests:
Your knowledge of collections.
Your ability to loop and count.
How cleanly you can filter and print results.
🏷️ Hashtags :
#JavaHashMap, #StringManipulation, #QAautomation, #CodingInterview, #BeginnerQA, #TestDataValidation, #JavaForTesters, #DuplicateCharacters, #CharacterCount, #HashMapExample, #CollectionsInJava, #MapConcepts, #JavaStringHandling, #TechForTesters, #FrequencyCounter, #QAProgramming, #AutomationLearning, #CodingTips, #CaseSensitive, #CodingPractice