0

I am new to java, and am confused with the problem. This is what I came up so far. I am still working on it, if I come up with any progress, I'll post it here.

public class charArray {

public static void main(String[] args) {

    String[] strArray = new String[] {"apple", "ball", "cat", "apple", "ball", "apple"};
    //output should be ["apple", "ball"]

    checkDuplicate(strArray);
}
public static String[] checkDuplicate(String[] strArray){
    String[] newArray = new String[]{};
    for(int i = 0; i < strArray.length; i++){

        for(int j = 0; j < i; j++){
            if (strArray[i].equals(srtArray[j])){
                newArray = strArray[i];
            }
        }

    }
    return newArray[];

}

}


New progress: Ok, my mediocre head has gone this far: (and the solution works) It prints out the unique array of Duplicate elements. But now I need to implement the same code, by calling a method. Any help is appreciated.

import java.util.*;

public class setChar {

public static void main(String[] args) {
    String[] strArray = new String[] {"apple", "ball", "cat", "apple", "ball", "apple"};
    Set set = new HashSet();
    Set uniqueSet = new HashSet();
    for(int i = 0; i < strArray.length ; i++ ){
        boolean b = set.add(strArray[i]);
        if(b == false){
            uniqueSet.add(strArray[i]);
        }
    }
    Iterator it = uniqueSet.iterator();
    while (it.hasNext()){
        System.out.println(it.next());
    }
}

}

The output is: ball apple


Finally implemented with method, with proper return type. Please let me know, if this can be further optimized. Thank all for you suggestions. Here is the working code:

public class retUnique {

public static void main(String[] args) {
    String[] strArray = new String[] {"apple", "ball", "cat", "apple", "ball", "apple"};

    System.out.println(printUnique(strArray));
}

public static Set<String> printUnique(String[] strArray){
    Set<String> set = new HashSet<String>();
    Set<String> uniqueSet = new HashSet<String>();
    for(int i = 0; i < strArray.length ; i++ ){
        boolean b = set.add(strArray[i]);
        if(b == false){
            uniqueSet.add(strArray[i]);
        }
    }
    return(uniqueSet);
}

}
jumbo
  • 36
  • 1
  • 1
  • 4
  • 1
    Could try with a LinkedHashSet (I believe it is?) which will only allow unique entries even when you add multiple entries that are the same. – OmniOwl Dec 18 '12 at 01:05
  • what you want ro return, the unique words, or ghe dupplicates? – AlexWien Dec 18 '12 at 01:06
  • AlexWien - I have to return unique words of just the duplicate words. – jumbo Dec 18 '12 at 01:07
  • This may have been answered already here http://stackoverflow.com/questions/13337296/in-java-is-there-a-more-elegant-way-to-remove-duplicate-strings-from-and-arrayl/13337369 – Opal Dec 18 '12 at 01:51
  • Thanks for the link Lee. But the problem is not the same. On the link you posted, the unique elements of the list is being sorted out. But on mine, I need a unique list of elements, that are Duplicate. Not all the elements in the list. Well I have a solution posted, please see if that can be optimized, and implemented in a method. – jumbo Dec 18 '12 at 03:27

3 Answers3

1

One easy option is to use a Set like container instead of an array. It will automatically ensure that only unique values are present.

You can look at examples of TreeSet and HashSet.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • ok.. I'm looking at the documentation now. But my java/programming skills are very rudimentary. – jumbo Dec 18 '12 at 01:10
  • thanks for the suggestion. I used HashSet and came up with a solution.. Please check if that can be optimized. I have edited the question part and added the solution there. – jumbo Dec 18 '12 at 03:12
  • @avas, i suggest you look at the answer by aug [here](http://stackoverflow.com/a/13924893/1520364). You dont need 2 sets because by the end of it, both sets will be identical, `set.add(strArray[i]);` will itself ensure that `set` only has unique strings – Karthik T Dec 18 '12 at 03:17
  • You can also use the function `Set::addAll` or the constructor itself to add all strings into the set instead of iterating. – Karthik T Dec 18 '12 at 03:19
  • Karthik, thanks for the suggestion. I might have not made it clear, the thing I need is, a Unique list of "Duplicates" not just unique list of all the elements. That is why I used two sets. I can not see how it can be done with just one set. – jumbo Dec 18 '12 at 03:32
  • @avas AH ok, its clearer now, I think most of that assumed its a typo. In that case you can also use something like [this](http://stackoverflow.com/a/5211215/1520364) to find duplicate elements, `Collections.frequency(..)>1`. – Karthik T Dec 18 '12 at 03:40
1

Something like this should work I believe. Been a while since I coded in Java so if anyone wants to make edits, please do.

public String[] returnDups(String[] strArray) {
    Set<String> set = new HashSet<String>(strArray);
    return set.toArray(new String[0]);
}

But what everyone has been suggesting is the correct idea. Sets make it really easy to remove duplicates so you should utilize them. Let's not reinvent the wheel.

aug
  • 11,138
  • 9
  • 72
  • 93
0

To detect dupplicates, if that is your question, add all elememts of the array to a Set, if set.add() returns false, then it is a dupplicate, so you can add this elemen to your result list of dupplictes.
Or if your question is meaning to return the values without duplicates, then you can retirnthe set, or convert the set to list before returning.

AlexWien
  • 28,470
  • 6
  • 53
  • 83