1

I'm trying to remove duplicate elements from a String array. For example if the input was Yellow, Yellow, Red. The output would be Yellow, Red. What do I put inside the conditional? Is there a remove method in java? Here's the method I made:

public static String [] CompareAndDestroy(String [] array)
{
String [] newarray = new String [array.length];
for(int i = 0; i<array.length;i++)
{
  for(int j = 0;j<array.length;j++) 
  {
    if(array[i].compareTo(array[j])==0)  
    {

    }
  }
 }
return array;
}
Patrick Jean
  • 45
  • 1
  • 3
  • 7
  • http://stackoverflow.com/questions/13812807/removing-duplicates-from-an-array-of-strings-without-explicit-comparison-in-jav – Abi Apr 02 '13 at 04:56

8 Answers8

4

If you need it to be and return a String[] array:

Set<String> stringSet = new HashSet<>(Arrays.asList(array));
String[] filteredArray = stringSet.toArray(new String[0]);

Although I'd consider changing the type to a Set<String> anyway since you're trying to store a list of unique elements.

NilsH
  • 13,705
  • 4
  • 41
  • 59
  • This code is really working,,, and one doubt ,, Whats the significance of **String[0]** in 2nd Line ? ? – Aneez Feb 18 '14 at 06:56
  • It's basically just to provide type information to the `toArray` method, as described in the [javadoc](http://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html#toArray(T[])) – NilsH Feb 18 '14 at 07:10
1

You can also try this ....

private static String[] arrRemove(String[] strArray) {
    Set<String> set = new HashSet<String>();
    set.addAll((List<String>) Arrays.asList(strArray));
    return (String[]) set.toArray(new String[set.size()]);
} 
Ashish Thukral
  • 1,445
  • 1
  • 16
  • 26
0

Store them in a hashset or linkedhashset. http://docs.oracle.com/javase/6/docs/api/java/util/Set.html

0

there is no method to remove from array. Since arrays are fixed size memory block, you can not remove or add any element. you can just assign values to elements. For your requirement, you can alternatively use, Hashset.

Ankit
  • 6,554
  • 6
  • 49
  • 71
0

Convert String[] to ArrayList < String >. ArrayList has a remove method. Check API doc.

rai.skumar
  • 10,309
  • 6
  • 39
  • 55
0

Unless this is an academic exercise, convert the elements to a Set instead.

Set<String> uniqueStr = new HashSet<>(Arrays.asList(array));
String[] buf = new String[0]; // zero-length typed array for Set.toArray operation
return uniqueStr.toArray(buf);  // returns all unique elements
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

You can put the elements in a set as it does not allow duplicates. Then convert that set into an array. LinkedHashSet is used so that you can get the new array in the same order as your original array.

String[] arr = new String[]{"Yellow","Red","Yellow","Green"};
Set<String> set = new LinkedHashSet<String>(arr);
set.toArray(new String[0]);
prasanth
  • 3,502
  • 4
  • 28
  • 42
0

Is there a remove method in java?

Please note: There is no delete or remove method in JAVA, C, C++ that remove element from simple array.

There are other containers like ArrayList, LinkedList,etc in JAVA from which you can delete any element(s).

What do I put inside the conditional to get unique elements?

You can either insert all the elements in a Hash-Set or you can create a new array (before first loop) and insert new elements in this new array.

Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71