Kodekraftt
/Blog
/Find Duplicate Characters In A String In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Find Duplicate Characters in a String in Java — Definition, Examples and Program

Learn how to find duplicate characters in a string using Java. This blog explains the concept, examples, and an efficient Java program using character frequency.

Find Duplicate Characters in a String in Java

Finding duplicate characters in a string means identifying characters that appear more than once. This problem is commonly asked in interviews and helps in understanding character frequency and string traversal.

Duplicate character detection is useful in:

  • Data validation
  • Password strength checking
  • Text analysis

How the Logic Works

  1. Create a frequency array to store character counts.
  2. Traverse the string character by character.
  3. Increment the count for each character.
  4. Print characters whose count is greater than 1.

Example 1

Input: "programming"

Duplicate Characters:

  • r
  • g
  • m

Example 2

Input: "Java"

Duplicate Characters:

  • a

Java Program to Find Duplicate Characters


import java.util.Scanner;

public class DuplicateCharacters {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String str = sc.nextLine().toLowerCase();

        int[] freq = new int[256];

        for (int i = 0; i < str.length(); i++) {
            freq[str.charAt(i)]++;
        }

        System.out.println("Duplicate Characters:");

        for (int i = 0; i < 256; i++) {
            if (freq[i] > 1 && i != ' ') {
                System.out.println((char) i);
            }
        }
    }
}

Sample Output


Enter a string: interview
Duplicate Characters:
i
e

Important Notes

  • Program is case-insensitive.
  • Spaces are ignored.
  • Time complexity is O(n).

Practice Challenges

  1. Print duplicate characters with their frequency.
  2. Find duplicate characters without using arrays.
  3. Print only the first repeating character.

Duplicate character detection strengthens your understanding of frequency counting and efficient string processing in Java.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.