user2435860
user2435860

Reputation: 798

How to construct a String without duplicate characters?

I am working on a personal project in Java and I am trying to build really fast a String without duplicates. Let me give you a specific example:

String s = null;

for (char c : tableChars) {
     s += c;
}

Ok, so I know that I can check if this character is already in the String, but I have to look for it a every insertion. Is there another way?

Upvotes: 0

Views: 447

Answers (6)

thePoly_glot
thePoly_glot

Reputation: 135

Have an character array length 36 of 26 chars+10 digits(0-9). Use collections.shuffle

Enough permutations to fit your need i hope.

p.s:Other option could be having a long unique string and manually just rotate the chars.really depends on how many strings you need to generate.

Upvotes: 0

Merlevede
Merlevede

Reputation: 8170

Your requirement is so particular that I doubt there's an already made function to do what you want. It seems like looking for the character at every insertion is the only way to go, but I can think of two approaches:

If your function will be handling relatively short strings, you look for a similar character at every insertion, looking in the string you're building.

For longer strings what you could do is have an array as big as your character set. Let's say you have 50 valid characters, then the size of your array would be 50.

//initialize array to all zeros
for (char c : tableChars)
{
    if (array[c] == 0)
    {
        s += c;
        array[c] = 1;
    }
}

Note that array[c] is just pseudocode, and you would need to adjust it to use the ASCII character as the index, or build some custom logic around it.

Upvotes: 0

Silvery
Silvery

Reputation: 1408

You can remove the duplicates from the tableChars first:

public static void main(String[] args) {
    char[] tableChars = {'G', 'o', 'o', 'd', 'l', 'u', 'c', 'k'};
    Set<Character> charSet = new LinkedHashSet<Character>();

    for (char c : tableChars){
        charSet.add(c);
    }

    StringBuilder sb = new StringBuilder();
    for (Character cr : charSet){
        sb.append(cr);
    }

    System.out.println(sb.toString());
}

The result:

Godluck

Upvotes: 0

matt b
matt b

Reputation: 139921

Any time you find yourself thinking "I want to check for duplicates in a very fast way", you should be using a HashSet or some other Set implementation.

Upvotes: 1

Ashot Karakhanyan
Ashot Karakhanyan

Reputation: 2830

Something like this:

Set<Character> charSet = new TreeSet<>();
charSet.add('a');
charSet.add('a');
charSet.add('b');

StringBuilder builder = new StringBuilder();
for (Character c : charSet) {
    builder.append(c);
}
System.out.println(builder.toString());

The result is: ab

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

You can try something with Set

    String str = "null";
    char[] arr=str.toCharArray();
    Set<String> set=new LinkedHashSet<>(); // LinkedHashSet keep the order
    for(char i:arr){
        set.add(String.valueOf(i)); // now you will have unique values
    }

Now

   System.out.println(set);

Out put:

   [n, u, l]

Upvotes: 5

Related Questions