Find first non repeated character in a String

One of the interview question is “How will you find first non repeating character in String.”
For example:
If input string is “analogy”,  then program should return ‘n’
If input string is “easiest”, then program should return ‘a’

First approach:

We will use LinkedHashMap to find first non repeating character in String.

Algorithm:

  • Get character while looping over String
  • Put this character in LinkedHashMap with count. If character is already there, increase count by 1.
  • Get count from LinkedHashMap while iterating. If count is 1,return that character as LinkedHashMap maintains insertion order.

Program:

When you run above program, you will get following output:

Second Approach:

Algorithm:

  • Iterate through each character of string.
  • If lastIndexOf and indexOf return same value, then it is first non repeating character in the String.

Program:

When you run above program, you will get following output:

Please go through Common java interview Programs for more such programs.


import_contacts

You may also like:

Related Posts

  • 10 September

    Change the font size of title in Matplotlib

    In Python, there are a lot of libraries that are used for data visualization. One of the libraries is the Matplotlib library. The Matplotlib library is a very good library when it comes to data visualisation in python. With the help of this library, many types of graphs and visual representations of the data can […]

  • Free movie apps for android
    08 September

    22 Best Free Movie Apps For Android

    Table of ContentsThe Most Promising Free Movie Apps for AndroidPopcornflixSony CrackleFreeFlix HQVuduTubi TVFilm RiseUKMovNowCrunchyrollCyberflix TVYidioPluto TVTea TVPlexMovies AnywhereOld MoviesCinema HDVideoMixKodiSnagfilmsIMDB OriginalsFree Movie Tube App for AndroidMegabox HDFrequently Asked Questions About Free Movie Apps for AndroidDo there exist free movie apps for Android?Which are the best free movie apps for Android?Is it safe to use the […]

  • Free live tv streaming sites
    08 September

    Best Free Live TV Streaming Sites in 2021

    Table of ContentsBest Free Live TV Streaming Sites in 2021Stream2WatchTVPlayerUSTVGO123TVPluto TVUStreamXumoOK Live TVAiry TVPlex Live TVDistro TVCrackstreamsSquid TVFree TVMX PlayerTV CatchupTime4TVPeacock TVYupp TVSports SurgeFrequently Asked Questions About Free Live TV Streaming SitesIs watching free live tv streaming sites possible on Android/Firestick?What are the best free live TV streaming sites?Are the free live tv streaming sites […]

  • Learn java students
    01 September

    Is It Promising for Students to Learn Java?

    Table of ContentsWhat Is Java?How It Was Developed?Reasons Students Should Learn JavaWhere Is It Used?Who Can You Work With?Where Exactly Can Java Be Learned? Coding has become a very important part of the job industry, taking over almost every profession. You are required to have a basic knowledge of languages like HTML and CSS, if […]

  • Write array to CSV in Python
    30 August

    Python write array to CSV

    Table of ContentsUsing the pandas.to_csv() function to write array to CSV fileUsing the numpy.tofile() function to write array to CSV fileUsing the numpy.savetxt() function to write array to CSV fileUsing the file-handling methods to write array to CSV fileUsing the writerows() function to write array to CSV file A CSV file is a simple text […]

  • Multiply in Python
    30 August

    Multiply in Python

    Table of ContentsMultiply operator in PythonMultiplying a string with an integerMultiplying all the elements of a listUse the for loop to multiply all the elements of a listUse the functools.reduce() function to multiply all the elements of a listUse the numpy.prod() function to multiply all the elements of a listUse the math.prod() function to multiply […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.