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
you can add the String array to the HashSet
Set<String> h = new HashSet<String>(Arrays.asList(new String[] { "a", "b" }));
this will get you unique String values. If necessary convert the HashSet back to array
String[] uniqueValues = h.toArray(new String[0]);

- 5,730
- 3
- 27
- 51
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));

- 6,136
- 12
- 51
- 73
Add them all to a Set and you'll get unique users. Then convert it back to array.

- 4,175
- 15
- 31
Sort it alphabetically. If any two adjacent entries are the same, you've found a duplicate.

- 21,443
- 3
- 45
- 53
-
-
Sorting the string consume additional resources. Try to find an algorithm that it's not do this. – Mihai8 Mar 12 '13 at 09:08
-
why do you need to sort it first? is it faster or what purpose does it serve? just a cent. – Oneb Mar 12 '13 at 09:14
-
because working with sorted array is much faster. Assuming you want to check for duplicate and add user then, you'll get O(1) + O(N) for unsorted and O(lgN) + O(lgN) for sorted array. You have to spend some time for initial sorting, but the benefits of fast checks will ovecome it. – Chechulin Mar 12 '13 at 09:27
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.

- 2,426
- 7
- 28
- 35
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).

- 6,554
- 6
- 49
- 71
-
O(N) _per user_ which is just as fast as adding first and searching for duplicates after... – Vincent van der Weele Mar 12 '13 at 09:17
-
If the set is unprepared then searching for duplicate will be slower than O(N). But if it is at least sorted you can add new user in O(lgN). – Chechulin Mar 12 '13 at 09:25
-
@Chechulin in what do u mean by unprepared set? as there is only single input to compare with, it shouldn't be more than O(N) in worst case. – Ankit Mar 12 '13 at 09:28
-
@Heuster yes, but if index of newly added result is not known it wont be O(n) in that case, it would be n2. moreover, its not a good design to 1st add then remove the unique-key element. – Ankit Mar 12 '13 at 09:34
-
1@ay89 my comment was adressed to Heuster's reply, describing that checking for existing user before adding new one is much better that performing checks after adding one. Btw, searching for duplicates can be made by O(NlgN) operations. – Chechulin Mar 12 '13 at 11:34
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.

- 3,213
- 1
- 18
- 22
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;
}

- 194
- 1
- 2