0

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?

user2435860
  • 778
  • 3
  • 9
  • 22
  • 2
    Remove the letters chosen from a set instead of iterating over the string. That said, there's likely a string length below which this isn't helpful--have you determined there's a bottleneck? – Dave Newton Feb 06 '14 at 15:21
  • A `HashSet` of strings will do the trick. – Omoro Feb 06 '14 at 15:22
  • Should it be random string with given length? Then you can just iterate and randomly choose char from set simultanously removing this char from the set – Jakub Kubrynski Feb 06 '14 at 15:23

6 Answers6

5

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]
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

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.

matt b
  • 138,234
  • 66
  • 282
  • 345
0

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

Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
0

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
Silvery
  • 1,358
  • 2
  • 8
  • 8
0

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.

Merlevede
  • 8,140
  • 1
  • 24
  • 39
0

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.

thePoly_glot
  • 135
  • 1
  • 2
  • 11