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
  • Use modified Mergesort, removing duplicates when encountered, instead of adding both copies back to the list. Runs in O(N*logN) Commented Oct 6, 2013 at 18:45

10 Answers 10

14

Quick but somewhat inefficient way would be:

Set<String> temp = new HashSet<String>(Arrays.asList(test));
String[] uq = temp.toArray(new String[temp.size()]);
Sign up to request clarification or add additional context in comments.

1 Comment

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?
5

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()] );

Comments

2

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.

Comments

2
String[] test = {"1","1","1","2"};
java.util.Set result = new java.util.HashSet(java.util.Arrays.asList(test));
System.out.println(result);

1 Comment

The ""+ part is not required, System.out.println(result) is all that's needed.
2

I tried all the answers on this page and none worked as-is. So, here is how I solved it, inspired by the answers from Taig and akuhn :

import groovy.io.*;
def arr = ["5", "5", "7", "6", "7", "8", "0"]
List<String> uniqueList = new ArrayList<String>( 
         new LinkedHashSet<String>( arr.asList() ).sort() );
System.out.println( uniqueList )

Comments

2

Just found a nicer way in Java 8 :

Arrays.stream(aList).distinct().toArray(String[]::new)

Comments

1

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

Comments

1
List list = Arrays.asList(test);
Set set = new HashSet(list);

String[] uq = set.toArray();

2 Comments

It would be easier to call Set.toArray().
Yeah for a moment I choose the loooong way dunno why
0

here is my solution:

int[] A = {2, 1, 2, 0, 1};

Arrays.sort(A);

ArrayList<Integer> B = new ArrayList<Integer>();

for (int i = 0; i < A.length; i++) {
 if (i == A.length-1) {
    B.add(A[i]);
 }
 else if (A[i] != A[i+1]) {
    B.add(A[i]);
 }
}

Comments

0
String[] getDistinctElementsArray(String[] arr){

    StringBuilder distStrings = new StringBuilder();
    distStrings.append(arr[0] + " ");
    for(int i=1;i<arr.length;i++){
        if( arr[i].equals(arr[i-1])){}
        else{
            distStrings.append(arr[i] + " ");
        }
    }
    return distStrings.toString().split(" ");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.