How do I remove duplicate names from an array in Java?
-
This is my code. The program takes in names until '-1' is entered and then it prints out the number of names and the names in the order they were entered. import java.util.Scanner; public class NameList { public static void main(String [] args) { String firstName; int count = 0; int nameSize = 0; String[]name = new String [20]; Scanner reader = new Scanner(System.in); System.out.print("Please enter a name: "); firstName = reader.nextLine(); while(!(firstName.equals("-1"))&&(na… < 20)) { name[nameSize] = firstName; nameSize = nameSize + 1; System.out.print("Please enter a name: "); firstName = reader.nextLine(); count++; } System.out.println(); System.out.println("The total number of names is: "+count); for(int index = 0;index < nameSize; index++) { System.out.print(name[index]+" "); } } } If one name is entered twice, how can I get the program to delete the duplicate entry?
-
Answer:
The easiest way is to eliminate duplicates before they get in the array to begin with...after getting each name, scan thru the array to see if it's already there and, if not, add it.
Yahoo! Answers Visit the source
Other answers
if u don't want any duplicates, then an array won't help u... u should use a Set... if u want ur Set sorted, you can use a TreeSet... otherwise just a HashSet would do... using these arent complicated... all u need to know is there in the java api... just check it out and look for TreeSet and HashSet under the classes list..and also, these are in the util package.. so import these before tryna use them... http://download.oracle.com/javase/6/docs/api/
Agree with the first guy. What you want is a Set, which is just an Array but without duplicate entries. Just use a HashSet: http://www.java2s.com/Tutorial/Java/0140__Collections/SetandHashSet.htm
Related Q & A:
- How can I change a value in an array?Best solution by Stack Overflow
- How do I define the index within the array?Best solution by Stack Overflow
- How can I remove duplicate Objects from array of Objects in javascript?Best solution by Stack Overflow
- How can I remove names from my messenger list?Best solution by Yahoo! Answers
- How do I get multiple names for me on myspace?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.