First unique character in a string, uniquecharacter

Source: Internet
Author: User

First unique character in a string, uniquecharacter

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


C-language standard library functions

Install the msdn c ++ Help File

JAVA statement simbolKey = CharactertoString (strs [0] charAt (0); convert to C #

SimbolKey = char. ToString (str [0]. First ());

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.