Write a Java program to Find common characters and unique characters in string

Write java code to count the common and unique letters in the two strings. If a character appears more than once in the 1st string. So ‘character’ should not be considered while computing the count value.

Following Rules to Use to check common character

  • Space should not be counted as a letter.
  • Consider letters to be case-sensitive. ie, “a” is not equal to “A”.
  • The return type of the output is the count of all common and unique characters in the two strings.

Sample Input and Output Format

  • Input consists of two strings.
  • Output is an integer.

Refer sample output for formatting specifications

Sample Input 1:

a black cow

battle ship

Sample Output 1:

2

[Explanation: b, l, and a are the common letters between the 2 input strings. But ‘a’ appears more than once in the 1st string. So ‘a’ should not be considered while computing the count value.]
Sample Input 2:

australia

sri lanka

Sample Output 2:

4

Java program to find common characters in String

Following are the steps to find common characters and unique characters in a string in Java

  • Input two strings.
  • Pass the string to the string buffer and remove the white space in both strings.
  • Use the nested loop to iterate over the string. Initialize count variable also.
  • Now, compare both the character at the specified position. If both the character matches then delete that character and increment the count value.
  • Now, once the inner loop exits, check the count value. If it is greater than 1, delete that character from the specified position.
  • Repeat the above steps for another string.
  • At last, use a nested loop to compare both the string and see if there is matched character, then increment count value.
  • Now, just print the count value, and hence you will get the common character count between two strings.

Java Code to find common characters in String

package com.demo;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        String s2 = sc.nextLine();
        StringBuffer sb1 = new StringBuffer(s1.replace(" ", ""));
        StringBuffer sb2 = new StringBuffer(s2.replace(" ", ""));
        for (int i = 0; i < sb1.length(); i++) {
            int c = 0;
            for (int j = i + 1; j < sb1.length(); j++) {
                if (sb1.charAt(i) == sb1.charAt(j)) {
                    sb1.deleteCharAt(j);
                    c++;
                }
            }
            if (c >= 1) {
                sb1.deleteCharAt(i);
            }
        }
        for (int i = 0; i < sb2.length(); i++) {
            int c = 0;
            for (int j = i + 1; j < sb2.length(); j++) {
                if (sb2.charAt(i) == sb2.charAt(j)) {
                    sb2.deleteCharAt(j);
                    c++;
                }
            }
            if (c >= 1) {
                sb2.deleteCharAt(i);
            }
        }
        int count = 0;
        for (int i = 0; i < sb1.length(); i++) {
            for (int j = 0; j < sb2.length(); j++) {
                if (sb1.charAt(i) == sb2.charAt(j)) {
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}

Output

Write a Java Program to find Unique Character in String

Following Rules to Use to check unique character

  • Space should not be counted as a letter.
  • Consider letters to be case-sensitive. ie, “a” is not equal to “A”.
  • The return type of the output is that contains unique characters in the two strings.

Sample Input and Output Format to find Unique character

  • Input consists of two strings.
  • Output is a string.

Following are the steps to find unique characters in a string in Java:

  • Input two String.
  • Remove whitespace if any.
  • Iterate over the string and first check for a duplicate character in a string. If found just delete that character using deleteCharAt() method. 
  • Repeat the same for the second string.
  • At last, just print the string without a common character.

Java code to find Unique Character in String

package com.demo;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        String s2 = sc.nextLine();
        StringBuffer sb1 = new StringBuffer(s1.replace(" ", ""));
        StringBuffer sb2 = new StringBuffer(s2.replace(" ", ""));
        for (int i = 0; i < sb1.length(); i++) {
            int c = 0;
            for (int j = i + 1; j < sb1.length(); j++) {
                if (sb1.charAt(i) == sb1.charAt(j)) {
                    sb1.deleteCharAt(j);
                    c++;
                }
            }
            if (c >= 1) {
                sb1.deleteCharAt(i);
            }
        }
        System.out.println("Unique Character in String 1 :"+sb1);
        for (int i = 0; i < sb2.length(); i++) {
            int c = 0;
            for (int j = i + 1; j < sb2.length(); j++) {
                if (sb2.charAt(i) == sb2.charAt(j)) {
                    sb2.deleteCharAt(j);
                    c++;
                }
            }
            if (c >= 1) {
                sb2.deleteCharAt(i);
            }
        }
        System.out.println("Unique Character in String 2 :"+sb2);
                
    }
}

Output

Thus, in this way, we learned how to find common characters and unique characters in strings in Java.