First unique character in a string

Source: Internet
Author: User
Tags first string

Question: Given a string, find the unique character in the first string. For example, "Cisco" should be I, and "cat" should be C.

The first method is to use linkedhashmap to keep the data order, repeat the string, count the characters, and then find the first character with a quantity of 1.

The second method is to use a hashset to store repeated characters, and then use an arraylist to store only the characters that appear once.

The third method is to use hashmap to store data and count, and then go through the string to check whether the count for each character is 1.


Code:

Import Java. io. ioexception; import Java. util. arraylist; import Java. util. hashmap; import Java. util. hashset; import Java. util. linkedhashmap; import Java. util. list; import Java. util. map; import Java. util. map. entry; import Java. util. set; public class firstuniquecharacter {// method 1 public static char getfirstnonrepeatedchar (string Str) {Map <character, integer> counts = new linkedhashmap <> (Str. length (); For (char C: Str. tochararray () {counts. Put (C, counts. containskey (c )? Counts. get (c) + 1: 1) ;}for (Entry <character, integer> entry: counts. entryset () {If (entry. getvalue () = 1) {return entry. getkey () ;}} throw new runtimeexception ("didn't find any non repeated character") ;}// method 2 public static char firstnonrepeatingchar (string word) {set <character> repeating = new hashset <> (); List <character> nonrepeating = new arraylist <> (); For (INT I = 0; I <word. length (); I ++) {char letter = word. charat (I); If (repeating. contains (letter) {continue;} If (nonrepeating. contains (letter) {nonrepeating. remove (character) letter); repeating. add (letter);} else {nonrepeating. add (letter) ;}return nonrepeating. get (0) ;}// method 3 Public static char firstnonrepeatedcharacter (string word) {hashmap <character, integer> scoreboard = new hashmap <> (); // build table [char-> count] For (INT I = 0; I <word. length (); I ++) {char c = word. charat (I); If (scoreboard. containskey (c) {scoreboard. put (C, scoreboard. get (c) + 1);} else {scoreboard. put (C, 1) ;}}// since hashmap doesn't maintain order, going through string againfor (INT I = 0; I <word. length (); I ++) {char c = word. charat (I); If (scoreboard. get (c) = 1) {return C ;}} throw new runtimeexception ("undefined behaviour ");}}





Reference: http://javarevisited.blogspot.com/2014/03/3-ways-to-find-first-non-repeated-character-String-programming-problem.html

First unique character in a string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.