Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to find the first character which occurs only once in string?

  1. #1
    Junior Member
    Join Date
    May 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow How to find the first character which occurs only once in string?

    I have a string "refer reference-book" and I want to find the first character which occurs only once, in this string I have 2 characters which occurs only once which is a space " " and a hyphen "-" but the first character is space " " so I want to return that from a method.

    How to do this?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to find the first character which occurs only once in string?

    Please define what you mean by "first character".

    The count of the occurrences of characters could be done with a Map<char, position>. If item already in Map, then it occurs more than once.
    Or by using an int array with the char value used as the index into the array: count['a']++ to count the letter 'a'. Then search the array for counts of 1.

    --- Update ---

    Also posted here: https://coderanch.com/t/731067/java/...-occurs-string
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 8
    Last Post: July 12th, 2014, 03:38 PM
  2. Sequences (convert char[] to String) [Replacing String with Character]
    By tpolwort in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:56 PM
  3. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  4. count how frequently each character occurs
    By sam in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2010, 02:16 PM