Reputation: 169
hello guys i am try to eliminate all the duplicate strings from a string of array and preserve only one string of that...
assume temp[] be the string array and contains
temp[0] = "stack";
temp[1] = "overflow";
temp[2] = "stack";
temp[3] = "stack";
temp[4] = "overflow";
i need only "stack" and "overflow" in this case (it may be stored into another string array) but preserving the order as
temp2[0] = "stack";
temp2[1] = "overflow";
assume temp2 be another string array..
Upvotes: 0
Views: 593
Reputation: 8809
A java.util.LinkedHashSet
will remove duplicates while preserving order.
temp = new LinkedHashSet<String>(Arrays.asList(temp)).toArray(new String[0]);
Upvotes: 4
Reputation: 2273
You ned to use a Set here:
Set<String> set = new HashSet<String>();
set.addAll(Arrays.asList(temp));
temp2 = set.toArray();
Upvotes: 0
Reputation: 240870
Use Set
Set<String> uniqueStrings = new HashSet<String>();
uniqueStrings.add("Stack");
uniqueStrings.add("Overflow");
uniqueStrings.add("Stack");//ignored
Upvotes: 2