Sunday, January 27, 2019

Java Program 3: Find first duplicate character in the given string

Let us understand the question first, we have to find the character in the given string which is repeated twice and first. Let us understand with examples:

  • Suppose "Selenium Java" is the given string, in this string 'a' and 'e' repeated twice but 'e' character coming first, so the first duplicate character will be 'e'.
  • Suppose "JEE2 JAVA" is the given string, in this string 'J', 'E" and 'A' repeated twice but 'J' character is coming first, so the first duplicate character is 'J'
Hope the question is clear now, let us implement the logic. We are going to use "HashMap" to solve this question.

Step 1: First we will convert the String to a Character Array

Step 2: Then add each character from the array to HashMap<K, V>  as a key, value pair. While adding the character verify if the character is already present then increase the value by 1, otherwise just add it to HashMap<K, V> with value 1.

Step 3: Finally read each character from the character array and verify whose value is equal to 2, the first matched character will be first duplicate character in the given string.

Execute the above program for different inputs and verify the results.

Some sample outputs are below:

Please let me know if above logic is failing for any of your inputs, I will verify and update. Thanks!!

#HappyLearning #HappyCoding


  1. Replies
    1. Great Article
      android based projects

      Java Training in Chennai

      FInal Year Project Centers in Chennai

      Java Training in Chennai

      projects for cse

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  2. great information.
    thank you for posting.
    keep sharing.
    Best java training in Bengaluru

  3. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai


  4. Thanks for your extraordinary blog. Your idea for this was so brilliant. This would provide people with an excellent tally resource from someone who has experienced such issues. You would be coming at the subject from a different angle and people would appreciate your honesty and frankness. Good luck for your next blog!
    Tally ERP 9 Training
    tally classes
    Tally Training institute in Chennai
    Tally course in Chennai
    seo training classes
    seo training course
    seo training institute in chennai
    seo training institutes
    seo courses in chennai
    seo institutes in chennai
    seo classes in chennai
    seo training center in chennai

  5. I really appreciate your method to explaining, I hope to see more posts from your blog. thank you!
    Digital Marketing Course In Kolkata
    Web Design Course In Kolkata
    SEO Course In Kolkata

  6. I'm going to share with you the exact ten-step process I take companies through when developing their staff to think and act like business owners who make customers want to buy, buy again, and then go on to tell their friends and family to buy! Salesforce training in Hyderabad


Selenium4: Working with two parallel windows in Selenium Automation

A new method newWindow() is introduced in Selenium 4 (Alpha). Using this method we can open a new tab or new window from the current window...