Java Program to Find Duplicate Characters in a String

To find duplicate characters in a string java we have two method . We can find repeated characters in a string java using brute force algorithm and map in java . Let’s see first java program to find repeated characters in a string using map .

Method 1- Find duplicate characters in a string Using Map

We can find duplicate characters in a string java using hashmap . We will follow bellow steps for print duplicate characters from string .

Algorithm

  1. Create a Hashmap<Character, Integer> character as key and count as value.
  2. If Hashmap contains character then increase count by 1 otherwise insert char with count 1.
  3. Iterate map and print key with value more then 1.
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class DuplicateCharsInString {
	public void findDuplicateChars(String str) {
		Map<Character, Integer> dupMap = new HashMap<Character, Integer>();
		char[] chrs = str.toCharArray();
		for (Character ch : chrs) {
			if (dupMap.containsKey(ch)) {
				dupMap.put(ch, dupMap.get(ch) + 1);
			} else {
				dupMap.put(ch, 1);
			}
		}
		Set<Character> keys = dupMap.keySet();
		for (Character ch : keys) {
			if (dupMap.get(ch) > 1) {
				System.out.println(ch);
			}
		}
	}

	public static void main(String a[]) {
		DuplicateCharsInString dcs = new DuplicateCharsInString();
		dcs.findDuplicateChars("javatipsall");
	}
}

Output:

a 
l 

 

Method 2 – Find duplicate characters in a string in java without using collection

We can write a simple java program to find duplicate characters in a string using two loops. In this approach we will follow these steps.

Algorithm:

  1. Iterate two loop outer loop start from first element with count 0.
  2. Inner loop start from next element from outer loop and check char is present .
  3. If char is present then increase count and set element ‘0’ as mark char is visit .
  4. Now if count is more than 1 , then char is repeated in string and we will print it.
public class PrintDuplicateCharactersFromString {
	public static void main(String[] args) {
		String string1 = "javatipsall";
		int count;

		// Converts given string into character array
		char string[] = string1.toCharArray();

       //outer loop
		for (int i = 0; i < string.length; i++) {
			count = 1;
			//inner loop
			for (int j = i + 1; j < string.length; j++) {
				if (string[i] == string[j] && string[i] != ' ') {
					count++;
			      // Set string[j] to 0 to avoid printing visited char
					string[j] = '0';
				}
			}
			// A char is considered as duplicate if count is greater than 1
			if (count > 1 && string[i] != '0')
				System.out.println(string[i]);
		}
	}
}

Output:

a
l

In this article we have discussed how to find duplicate characters in a string java using hashmap and how to find duplicate characters in a string in java without using collection . I hope we can find repeated characters in a string java now using these method. This article is a part of Java String Interview Program  , there you can learn more string interview question .

Leave a Reply

Your email address will not be published. Required fields are marked *

9 + 1 =