0

I am trying to search through an array and pick out all of the unique strings meaning only one instance of each string is being printed. Then I want to them print them out. However, it is not detecting any unique strings. Could someone help me figure out what is wrong?

Note: Essentially, I am trying to count how many strings there are, without counting duplicates.

public class Test {
public static String uniquecenter;

public static void main (String[] args){


    //Grab Data and Feed into Centers
    String centers[]=new String[1000];


    /* Only for Testing Purposes*/
    centers[0] = "Table Tennis";
    centers[1] = "Soccer";
    centers[2]= "Baseball";
    centers[3] = "Table Tennis";

    //Unique Centers
    String uniquecenters[]=new String [1000];


    //0 out array
    for(int i=0; i<1000;i++){
        uniquecenters[i]="0";
    }


    //loop through center array
    for(int i=0; i <1000; i++){

        boolean unique=false;


        //Loop through unique centers
        for(int l=0; l<1000; l++){

            //if it finds that this point in the centers array already has been indexed in the unique centers
            //array then it is not unique...so move on to the next center
            if(uniquecenters[l].equals(centers[i])){
                unique=true;
            }


        }

            //if it never found this string in the uniquecenters array...
            if(unique==false){

                //find an empty spot in the unique array
                for(int l=0;l<1000;l++){

                    //grab the unique centers string
                    String c=uniquecenters[l];

                    //check if something is already in this spot...if not then...
                    if(uniquecenters.equals("0")){  
                        //..make it a unique center
                        uniquecenters[l]=uniquecenter;
                    }//End of placing center
                }//end of looking for unique spot
            }//end of if it is unique


    }//end of creating this unique center array

    //print all unique strings
    for(int i =990; i>=0;i--){

        String print =uniquecenters[i];

        //if it is emptry
        if(print.equals("0")){

        }else{
            //print this unique center
            System.out.println(print);
        }
    }
}

}

6
  • Why not just use the Strings as keys for a HashMap? Commented Apr 27, 2014 at 22:38
  • How can I find unique strings using a hashmap? Commented Apr 27, 2014 at 22:40
  • @AntonH This is actually a much better fit for a Set. There are a number of problems with this code, but stylistically I immediately note the strange use of the word unique and the use of hard-coded limits instead of array.length. Commented Apr 27, 2014 at 22:43
  • Create a HashMap of type <String, Integer>. loop through your array, using the String as the key, and increment the value of that element in the HashMap. Then, go through the HashMap, and the keys where the value is 1 are unique. Commented Apr 27, 2014 at 22:43
  • @chrylis Sets would remove the second (and third, etc) copy of a String, but not necessary say if it's unique. Or you have a way of doing it that I haven't thought of. Also, I honestly didn't even look through the code, so I haven't looked at any other programming or logic errors. EDIT Given OP's edit, yes, Sets would probably be better. Commented Apr 27, 2014 at 22:46

1 Answer 1

3

You can use a Set which, by definition, does not contain duplicates:

String centers[];

...

List<String> centerList = Arrays.asList(centers);
Set<String> uniqueCenters = new HashSet<String>();
uniqueCenters.addAll(centerList);
uniqueCenters.remove(null);
Integer numberOfUniqueStrings = uniqueCenters.size();
Sign up to request clarification or add additional context in comments.

1 Comment

This code most likely will add unexpected null to the set.

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.