1

How can I remove duplicate strings from a string array without using a HashSet?

I try to use loops, but the words not delete.

StringBuffer outString = new StringBuffer("Our, aim, and, isn't, easy, you, you're, actual, and, are, aren't, and, improve, achieving, and, Obviously, and, illumination, are");

wordList = outString.toString().split(", ");
for (i = 0; i < wordList.length; i++) {
  for (j = 0; j < wordList.length; j++) {
    if((wordList[i]!=wordList[j])&&(j>i)){
      t=true;
    }
  }
  if(t==true){
    k++;
  }
}
String[] wordList1 = new String[k];
wordList = outString.toString().split(", ");
for (i = 0; i < wordList.length; i++) {
  (j = 0; j < wordList.length; j++) {
    if((wordList[i]!=wordList[j])&&(j>i)){
      t=true;
    }
  }
  if(t==true){
    wordList1[i]=wordList[i];
  }
}
5
  • 2
    And why is using a Set out of the picture? Also, I can't understand how you're trying to remove in your example. Commented Apr 23, 2013 at 17:22
  • 1
    Please give an example value for outString. Commented Apr 23, 2013 at 17:22
  • It is part of my work( Commented Apr 23, 2013 at 17:25
  • You always end up setting t to true, meaning that all the words get copied. Look at the logic of your loop. t is never set to false again! Also, since you have a j>i condition, why not start the second loop at i+1 instead of at 0. There are many other issues with your code... Commented Apr 23, 2013 at 17:27
  • @Duncan Jones StringBuffer outString = new StringBuffer("Our, aim, and, isn't, easy, you, you're, actual, and, are, aren't, and, improve, achieving, and, Obviously, and, illumination, are"); Commented Apr 23, 2013 at 17:31

9 Answers 9

4

1) I think you need to use the equals operator. Try

if (!wordList[i].equals(wordList[j])){

instead of !=.

2) Also Kevin is right. You need to set t back to false.

3) Side note as pointed out by others already: To be more efficient you should start the inner loop with

for (j = i+1; j < wordList.length; j++) {

4) Another side note: Your result array is still too long. If you don't want to use a List<String> and it is ok to loose the original array you could go with a solution as suggested by Zim-Zam O'Pootertoot and set the original duplicates to null, add a counter to count how many null values you assigned, initialize the new array with the correct size and loop a final time over the first array and copy only the non-null values into your new array.

Sign up to request clarification or add additional context in comments.

2 Comments

I dont understand only this part.
which part is difficult? try to describe what you tried and what your results are. Otherwise nobody knows what the problems are you struggle with. Try to be more specific.
3

Try this code to remove dup words:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < wordList.length; i++) {
    boolean found = false;
    for (int j = i+1; j < wordList.length; j++) {
        if (wordList[j].equals(wordList[i])) {
            found = true;
            break;
        }
    }
    // System.out.printf("Checking: [%s]%n", wordList[i]);
    if (!found) {
        if (sb.length() > 0)
            sb.append(' ');
        sb.append(wordList[i]);
    }
}
System.out.printf("Unique: [%s]%n", sb);

Comments

3

If you are allowed to use Lists, you can define a generic method that does this fairly easily:

public <T> T[] removeDuplicates(final T[] array) {
    List<T> noDuplicates = new ArrayList<T>();
    for (T arrayElem : array) {
        if (!noDuplicates.contains(arrayElem)) {
            noDuplicates.add(arrayElem);
        }
    }
    return (T[]) noDuplicates.toArray();
}

Comments

2

You probably want to set t back to false after pulling the value you want:

if(t)
{
     wordList1[i]=wordList[i];
     t = false;
}

Also this:

if((wordList[i]!=wordList[j])&&(j>i))

Will always return true since strings are immutable (unless you compared a string to an exact reference of itself which you disallow with j>i). You need to change it to say this:

if (!(wordList[i].equals(wordList[j]))&&(j>i))

Using .equals will compared that they contain the same string, not that they point to the exact reference of a string.

Not sure if that's the only problems or not, a bit unclear from what's given.

Comments

0

In your inner loop, initialize j = i + 1

if(wordlist[i] != null && wordlist[i].equals(worldlist[j])) { wordlist[j] = null; }

...and then compact the array when you're finished to remove all null values

Comments

0

How about using a List:

wordList = outString.toString().split(", ");
List<String> finalList = new ArrayList<String>();
for(String val : wordList) {
  if(!finalList.contains(val)) {
    finalList.add(val);
  }
}

A Set would be more efficient, however. If you can't use a List or a Set, and you are forced to remove the duplicates, then you will have to loop through the array each time, which will perform horribly.

2 Comments

Oh. Also i cant use List<>
Then why not replace the List with a simple Array? You will have to manually loop the array you are adding to to see if the value already exists. You could write a function for that.
0

Iterate through the array, and store in an auxiliary int[] or List<Integer> the indexes of duplicates that you find with your two for's.

Create a new Array, with size equal to the original one minus the size of the repeated Strings.

Iterate through your original array, if the index isn't on your auxiliary list, set it to your new Array.

Comments

0

The best and most effective method is to suppose arr is the array that contains strings and can have duplicate values:

Arrays.sort(arr);
int l = 0;
for (int a = 0; a < arr.length; a++) {
    if (a == arr.length - 1)
        l++;// its a unique value
    else if (!(a[a + 1].equals(arr[a])))
        l++;// its also a unique
}
String newArray[] = new String[l];
l = 0;
for (int a = 0; a < arr.length; a++) {
    if (a == arr.length - 1)
        newArray[l] = arr[a];
    else if (!(a[a + 1].equals(arr[a]))) {
        newArray[l] = arr[a];
        l++;
    }
}

1 Comment

I guess it would be more effective, if it wouldn't have syntax errors.
0

Try this...

public class RemoveDupsStringArray {

public static void main(String[] args) {
    String[] withDuplicates = new String[] {"one","one","two","three","one","three","three"};
    String[] withoutDuplicates = new String[] {"one","two","three"};

    removeDuplicates(withDuplicates);
    removeDuplicates(withoutDuplicates);
}

private static void removeDuplicates(String[] array) {
    int[] occurence = new int[array.length];
    for (int i = 0; i < array.length; i++) {
        for(int j=i+1;j<array.length;j++){
            if(array[i]==array[j]){
                occurence[j]=j;
            }
        }
    }
    int resultLength=0;
    for(int i=0;i<occurence.length;i++){
        if(occurence[i]==0){
            resultLength++;
        }
    }
    String[] result=new String[resultLength];
    int index=0;int j=0;
    for(int i=0;i<occurence.length;i++){
        index = occurence[i];
        if(index==0){
            result[j]= array[i];
            j++;
        }
    }

    for(String eachString : result){
        System.out.println(eachString);
    }
}
}

1 Comment

While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.

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.