Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 01/14/2025 By admin

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:

package StringPrograms;

import java.util.Scanner;

public class FindDistinctCharInString {

        public static void findPrintDistinctCharInString(String inputString) {

                // Converting input string to upper case
                inputString = inputString.toUpperCase();
                
                // Removing all white spaces
                inputString = inputString.replace(" ", "");

                // We need to keep counting char till its length is greater than 0
                while (inputString.length() > 0) {
                        // Extracting first char
                        char c = inputString.charAt(0);
                        /*
                         * 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.
                         */
                        if(inputString.lastIndexOf(c)==0)
                                System.out.print(c+" ");
        
                        // Remove char whose occurrence is checked
                        inputString = inputString.replace(Character.toString(c), "");
                }
                

        }

        public static void main(String[] args) {

                // User input for the string to know distinct characters
                Scanner sc = new Scanner(System.in);
                System.out.println("Please enter the string to find distinct characters:");

                String userInput = sc.nextLine();

                sc.close();

                System.out.println("You entered: " + userInput);

                findPrintDistinctCharInString(userInput);
        }

}

Output:

Please enter the string to find distinct characters:Make Selenium EasyYou entered: Make Selenium Easy

K L N I U Y

Uncategorized

Post navigation

Previous Post: Different Ways Of Iterating Map In Java – Including forEach() and stream() Of Java 8
Next Post: path variables in postman

Related Posts

September 9, 2018 – Make Selenium Easy Uncategorized
Is It Possible To Use Page Factory Without FindBy In Selenium WebDriver? | Make Selenium Easy Uncategorized
Wildcard characters in XPath – Selenium WebDriver Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
staleExc – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark