Frequently Asked Java Program 30: Java Program to Find Distinct Characters In Given String

Hello Folks,

As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will see a java program to Find Distinct Characters In Given String.

Problem Statement:

User input: Make Selenium Easy

Output: K L N I U Y

Problem Solution: 

Logic:

  1. Convert input string in to same case either upper or lower.
  2. Replace all whitespace with empty space. 
  3. Take first char from given String.
  4. lastIndexOf(char c) method of String class gives the index of last occurrence of char in given string. If a character is not repeated in given string,  lastIndexOf should give zero as we took first char to check. If it is zero, print.
  5. To repeat step 3 and 4, we must need to replace already checked char from string with empty space.

Java Program:

Output:

Please enter the string to find distinct characters:
Make Selenium Easy
You entered: Make Selenium Easy
K L N I U Y

Run program for multiple inputs and let me know if it fails. Comment if you have better approach. If you like my posts please like, comment and share to reach it to maximum.

#HappyCoding

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning