Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert an array of strings to a array of unique values?

Tags:

java

arrays

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"}
like image 601
djangofan Avatar asked Dec 08 '09 01:12

djangofan


4 Answers

Quick but somewhat inefficient way would be:

Set<String> temp = new HashSet<String>(Arrays.asList(test));
String[] uq = temp.toArray(new String[temp.size()]);
like image 61
ChssPly76 Avatar answered Oct 16 '22 02:10

ChssPly76


If you're going with the HashSet-approach (which seems pretty handy) you should use a LinkedHashSet instead of a HashSet if you want to maintain the array's order!

Set<String> temp = new LinkedHashSet<String>( Arrays.asList( array ) );
String[] result = temp.toArray( new String[temp.size()] );
like image 31
Taig Avatar answered Oct 16 '22 00:10

Taig


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.

like image 2
Stephen C Avatar answered Oct 16 '22 00:10

Stephen C


String[] test = {"1","1","1","2"};
java.util.Set result = new java.util.HashSet(java.util.Arrays.asList(test));
System.out.println(result);
like image 2
maximdim Avatar answered Oct 16 '22 00:10

maximdim