HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Java – Find duplicate characters in String

Java – Find duplicate characters in String

This article presents a simple Java program to find duplicate characters in a String. This can be a possible Java interview question while interviewer may be evaluating your coding skills.

You can use this code to find repeated characters or modify the code to find non-repeated characters in string.

Find duplicate characters in string

Pseudo steps

  1. Split the string into character array.
  2. Iterate over character array.
  3. For each iteration, use character as map key and check is same character is present in map, already.
  4. If map key does not exist it means the character has been encountered first time. Store it in map with count value to 1.
  5. If map key exist, increment the counter.
  6. Repeat until all characters in array has been iterated.
  7. Check map. Duplicate characters have count more than 1.
  8. Distinct characters will have count as 1.

Java program to find duplicate characters in string

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

public class StringExample 
{
	public static void main(String[] args) 
	{
		String blogName = "howtodoinjava dot com";
		char[] chars = blogName.toCharArray();
		
		Map<Character, Integer> map = new HashMap<>();
		for(char c : chars) 
		{
			if(map.containsKey(c)) {
				int counter = map.get(c);
				map.put(c, ++counter);
			} else {
				map.put(c, 1);
			}
		}
		
		System.out.println("Duplicate characters:");
		
		//Print duplicate characters
		for(char c : map.keySet()) {
			if(map.get(c) > 1) {
				System.out.println(c);
			}
		}
		
		System.out.println("Duplicate characters excluding white space :");
		
		//Print duplicate characters excluding white space
		for(char c : map.keySet()) {
			if(map.get(c) > 1 && !Character.isWhitespace(c)) {
				System.out.println(c);
			}
		}
		
		System.out.println("Distinct characters:");
		
		//Print distinct characters
		for(char c : map.keySet()) {
			if(map.get(c) == 1) {
				System.out.println(c);
			}
		}
	}
}

Program output:

Duplicate characters:
 
a
d
o
t
Duplicate characters excluding white space :
a
d
o
t
Distinct characters:
c
h
i
j
m
n
v
w

We learned how we can use a map to find repeated character in string; also check non-repeated characters as well.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces