I have an array of string, which holds the list of strings. I want to figure out is there any duplicate entries in this list. Basically i have a list of users, and there should be no duplicate entries.
9 Answers
If you need unique things then we have Set in java
String[] users = "User1,User2,User1,User,User".split(",");
Set<String> uniquUsers = new HashSet<String>();
for (int i = 0; i < users.length; i++) {
if (!uniquUsers.add(users[i]))
users[i] = "Duplicate"; // here I am assigning Duplicate instead if find duplicate
// you can assign as null or whatever you want to do with duplicates.
}
System.out.println(Arrays.toString(users));
Comments
Sort it alphabetically. If any two adjacent entries are the same, you've found a duplicate.
4 Comments
If you want to make check on adding new user you just have to run through the array and use username.equals(*) on each existing user.
If you have an array with duplicate entries just run this algorythm for every user you have.
These are rough methods, there are a lot of optimizations for this problem.
Comments
as you mentioned, there should not be duplicate entry, so better you iterate the whole array before adding new user, rather than adding and then checking for duplicates. the former solution would solve it in O(N).
5 Comments
Patashu's idea seems the simplest. You can use Arrays.sort() to easily and efficiently sort the array.
If you really want to SEARCH, you probably would use one of the Arrays.binarysearch() methods. But they also require sorted arrays.... For each element in your array (say at index n), search the portion 0...(n-1) and also search the portion (n+1)...(length-1) but that would be grossly wasteful if you could just compare with one element adjacent to n. So it's back to the previous suggestion.
If you want to do slightly less coding, probably at the expense of speed, you could use the contains() method of one of the implementing classes of AbstractCollection - probably ArrayList (can contain duplicates), TreeSet (sorted, contains unique values) or HashSet (unsorted, contains unique values). You can call the constructor for these collections with the parameter Arrays.asList(yourArray) so you don't need to populate one-by-one.
As ay89 rightly mentions, it is simpler to have an array with unique values (a set in other words), then check if your value is already contained before trying to add it. Makes things lot simpler. But you might not always have that luxury with what you are given.
Comments
create an array news_data and add strings in that.
for (int i = 0; i < news_data.length; i++) {
for (int j = i+1; j < news_data.length; j++) {
if(news_data[i].equals(news_data[j])){
news_data = removeElement(news_data, j);
}
}
}
public static String[] removeElement(String[] original, int element){
String[] n = new String[original.length - 1];
System.arraycopy(original, 0, n, 0, element );
System.arraycopy(original, element+1, n, element, original.length - element-1);
return n;
}
Comments
Very simple, use LINQ to find duplicate in your list.