tags:

views:

109

answers:

5

In Java, how do I convert an array of strings to a array of unique values?

If I have this array of Strings:

String[] test = {"1","1","1","2"}

And I want to end up with:

String[] uq = {"1","2"}
+1  A: 

An easy way is to create a set, add each element in the array to it, and then convert the set to an array.

Anon.
+1  A: 
List list = Arrays.asList(test);
Set set = new HashSet(list);

String[] uq = set.toArray();
victor hugo
It would be easier to call `Set.toArray()`.
Anon.
Yeah for a moment I choose the loooong way dunno why
victor hugo
+4  A: 

Quick but somewhat inefficient way would be:

Set<String> temp = new HashSet<String>(Arrays.asList(test));
String[] uq = temp.toArray(new String[temp.size()]);
ChssPly76
Why is it inefficient? Considering the array probably has more than four values. The alternative is to sort the array and look for dupes, right?
Per Wiklander
+2  A: 
String[] test = {"1","1","1","2"};
java.util.Set result = new java.util.HashSet(java.util.Arrays.asList(test));
System.out.println(result);
maximdim
The ""+ part is not required, System.out.println(result) is all that's needed.
daveb
Right, was in a hurry :)
maximdim
+1  A: 

An alternative to the HashSet approach would be to:

  1. Sort the input array

  2. Count the number of non-duplicate values in the sorted array

  3. Allocate the output array

  4. Iterate over the sorted array, copying the non-duplicate values to it.

The HashSet approach is O(N) on average assuming that 1) you preallocate the HashSet with the right size and 2) the (non-duplicate) values in the input array hash roughly evenly. (But if the value hashing is pathological, the worst case is O(N**2) !)

The sorting approach is O(NlogN) on average.

The HashSet approach takes more memory on average.

If you are doing this infrequently OR for really large "well behaved" input arrays, the HashSet approach is probably better. Otherwise, it could be a toss-up which approach is better.

Stephen C