Convert a String to an Array and Back Again without Duplicates

I am looking for the best way to convert a String to an Array and Back Again without any Duplicates.

Basically I have a comma delimited string that includes names.  I want to return a string that contains a comma delimited string but without any duplicates.
lasuttonAsked:
Who is Participating?
 
CEHJCommented:
Sorry:

array = (String[])new ArrayList<String>(new LinkedHashSet<String>(new ArrayList<String>(Arrays.asList(array)))).toArray(new String[0]);
0
 
CEHJCommented:
Try
            List unique = new ArrayList(new HashSet(new ArrayList(Arrays.asList(array))));
0
 
UrosVidojevicCommented:
You will have to use
String[] split(String regex)
method in String class and then remove duplicates manualy.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
lasuttonAuthor Commented:
And to move the list back into a delimited string, do I need to loop through each element and add to the new String?
0
 
lasuttonAuthor Commented:
For the...

array = (String[])new ArrayList<String>(new LinkedHashSet<String>(new ArrayList<String>(Arrays.asList(array)))).toArray(new String[0]);

...solution, I then parse through the array and move it into a String manually, or is there something that can do the whole thing in a single line?
0
 
CEHJCommented:
>>...solution, I then parse through the array and move it into a String manually, or is there something that can do the whole thing in a single line?

Yes, you do. Unfortunately there's no 'join' method in Java
0
 
lasuttonAuthor Commented:
Thanks CEHJ, this works perfectly based on your help:

public String removeRepeatingElementsInDelimitedString(String aStringToParse) throws Exception {
      String[] originalList = aStringToParse.split(",");
      String noduplicatesListDelimitedResult = "";
      originalList = (String[])new ArrayList<String>(new LinkedHashSet<String>(new ArrayList<String>(Arrays.asList(originalList)))).toArray(new String[0]);
      int newListLength = originalList.length;
      for(int x = 0;x<newListLength;x++)
      {
      noduplicatesListDelimitedResult = noduplicatesListDelimitedResult + originalList[x] + ",";
      }
      noduplicatesListDelimitedResult = noduplicatesListDelimitedResult.substring(0, noduplicatesListDelimitedResult.length()-1);
            
      return noduplicatesListDelimitedResult;
}
0
 
CEHJCommented:
Good - it would be better to append using a StringBuilder though
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.