How can I create a method that uses a HashSet and will return the unique characters in a string?
for example
unique("racecar") ----> "e"
unique("confuse") ----> "confuse"
Thank you.
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.
How can I create a method that uses a HashSet and will return the unique characters in a string?
for example
unique("racecar") ----> "e"
unique("confuse") ----> "confuse"
Thank you.
hmm... I don't think a HashSet is the best way to do this. Rather, I would use a Map (preferably a TreeMap). Use characters as the key, and an integer counter of how many times that character occurred. Then, iterate through all the characters of the String. See if the character is already in the Map. If it is, then simply increase the associated count. Otherwise, add a new key/value pair containing that character and an initial count of zero. At the end, simply iterate through all the items and throw out all characters which have a count not equal to 1.
Thank you for the input, however, I am still unsure as to what the actual code for this would look like. Can anyone provide some code for me?
Look at the HashSet add method for one tool to use.
This is what I have so far, however I cannot figure out how to actually get the code to return the unique characters.import java.util.TreeMap; public class IFailAtJava { public static void main(String[] args){ } public void unique(String[] s){ String[] word = s; TreeMap<String, Integer> map = new TreeMap<String, Integer>(); for(int i = 0; i < word.length; i++){ String key = word[i]; if(map.get(key) == null){ map.put(key, 1); } else{ int value = map.get(key).intValue(); value++; map.put(key, value); } } System.out.println(map); } }
Follow the last sentence:
Hint: use the iterator of the the TreeMap and a while loop. You can keep the same collection, or place all items left into a new collection (e.g. a List or a Set).At the end, simply iterate through all the items and throw out all characters which have a count not equal to 1.
import java.util.TreeMap; public class IFailAtJava { public static void main(String[] args) { } public String[] unique(String[] s) { String[] word = s; TreeMap<String, Integer> map = new TreeMap<String, Integer>(); for (int i = 0; i < word.length; i++) { String key = word[i]; if (map.get(key) == null) { map.put(key, 1); } else { int value = map.get(key).intValue(); value++; map.put(key, value); } for (int j = 0; j < word.length; j++) { if (map.get(key) > 1) { map.remove(key); } } } return word; } }
This is what I have added to the method, however I cannot get it to return anything without causing return type problems.
Are you trying to find a unique set of strings, or a unique set of characters within a single string?
Currently it looks like you're trying to do the first. Also, you're returning the original list of strings, not the left-over set. Also, you want the second loop to occur after the first, not inside. Use iterators to loop through the resultant map. If you want an array at the end, create an ArrayList and rather than explicitly removing items, just add the items with a count of 1 to that list, and call the toArray() method for the ArrayList (or change the return type of unique to return an ArrayList).
Look at using a second HashSet to save the dup entries discovered when adding to a first HashSet. After all strings added to the first, use the removeAll method to generate a unique list.