• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Jira Selenium Protractor Testing SDLC Agile Methodology

Java Selenium Tutorial & Testing Interview Questions

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Test Cases
  • Selenium
  • Manual Testing
  • Difference
  • Search
SoftwareTestingo » Java Programs » How to Find Non-Repeated Character & First Repeated Character In Java ?

How to Find Non-Repeated Character & First Repeated Character In Java ?

Last Updated on: March 15, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Non-Repeated & First Repeated Character Using HashMap

Non-Repeated & First Repeated Character Using HashMap

How to Find First Repeated & Non-Repeated Character In Java?

Check Also: Add Values In HashTable
package com.softwaretestingblog.programs;
import java.util.HashMap;
import java.util.Scanner;
public class Non_Repeated_Character {
   static void firstRepeatedNonRepeatedChar(String inputString)
   {
      //Creating a HashMap containing char as a key and occurrences as a value
      HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();
      //Converting inputString to char array
      char[] strArray = inputString.toCharArray();
      //Checking each char of strArray
      for (char c : strArray)
      {
         if(charCountMap.containsKey(c))
         {
            //If char is present in charCountMap, incrementing it's count by 1
            charCountMap.put(c, charCountMap.get(c)+1);
         }
         else
         {
            //If char is not present in charCountMap,
            //adding this char in charCountMap with 1 as it's value
            charCountMap.put(c, 1);
         }
      }
      //checking for first non-repeated character
      for (char c : strArray)
      {
         if (charCountMap.get(c) == 1)
         {
            System.out.println("First Non-Repeated Character In '"+inputString+"' is '"+c+"'");
            break;
         }
      }
      //checking for first repeated character
      for (char c : strArray)
      {
         if (charCountMap.get(c) > 1)
         {
            System.out.println("First Repeated Character In '"+inputString+"' is '"+c+"'");

            break;
         }
      }
   }
   public static void main(String[] args)
   {
      @SuppressWarnings("resource")
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the string :");
      String input = sc.next();
      firstRepeatedNonRepeatedChar(input);
   }
}
Check Also: Two Max Numbers From Array

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Tutorials Important Links

    • Software Testing Jobs
    • Manual Testing Tutorial
    • Selenium Tutorial
    • Core Java Tutorial
    • TestNG Tutorial
    • Java Programs
    • Selenium Programs
    • Manual Test Cases
    • Interview Tips
    • Contact US
    • www.softwaretestingo.com

    Important Links

    • Software Testing Interview Questions
    • Agile Interview Questions
    • Manual Testing Interview Questions
    • Testing Interview Questions
    • Selenium Interview Questions
    • Selenium Real Time Interview Questions
    • Selenium WebDriver Interview Questions
    • Automation Framework Interview Questions
    • Appium Interview Questions
    • TestNG Interview Questions
    • Java Interview Questions
    • Core Java Interview Questions

    Categories

    Copyright © 2021 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy