2

I was asked to write a program to print values in a character array. This array contains duplicated values, but output should not contain duplicated characters.Do not use Set. This is what I have created. Let me know if there is any other and efficient way to do the same.

public class RemoveDuplication {
  public static void main (String[] args){
    char[] a= new char[]{'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'};
    String s=Character.toString(a[0]);

    for (int i=1; i<a.length; i++) {
      if ((s.indexOf(a[i])) == -1) {
        s = s + Character.toString(a[i]);
      } else {
      }
    }
    // if you want character array as result 
    char[] result = s.toCharArray();
    System.out.println(result);
  }
}
2
  • 1
    System.out.println(Arrays.toString(result)); Commented Feb 25, 2017 at 18:25
  • 1
    Hi Elliott, Thanks for suggestion. Please note that we have overloaded 'println' method which accepts char array as argument and prints list of values in char array. Commented Feb 25, 2017 at 18:30

5 Answers 5

1

You are over-doing.

Just go for:

StringBuilder builder = new StringBuilder().append(a[0]);

and then append() to that builder object; and in the end; call builder.toString(). Instead of all the funny things you do with that s string variable.

Your code that is going from and back between String and Character and using + for string appending is as said; very much over-complicating things.

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

4 Comments

Yes ! Thanks for your suggestion buddy (y)
You are very welcome ... and funny thing: I assumed you wouldn't be allowed to use a set, that is why i didn't mention that part; as you already got that part to find the duplicates correctly ;-)
hi, how to know if the the given character already present in the string builder object. if ((s.indexOf(a[i])) == -1) this line of code is not valid if s is StringBuilder.
You could always do: builder.toString().indexOf(), too
1

If you use a Set object, it will do that for you.

Set<Character> s = new HashSet<>();
s.add('c');
s.add('c');
//c was only added once

then iterate like this:

for(Character c: s)
{
    System.out.println(c);
}

1 Comment

Yeahh ! its great solution but i was not allowed to use Set :(
0

Stream the characters, filter to only distinct code points, then collect the code points in a StringBuilder and finally print it out:

System.out.println(
    str.chars().distinct().boxed()
        .collect(Collector.of(StringBuilder::new,
                    StringBuilder::appendCodePoint,
                    StringBuilder::append)));

Comments

0

Each time I hear unique elements, Sets comes to my mind. So here is the easiest implementation using sets:

char[] a= new char[]{'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'};

Set<Character> set = new HashSet<Character>(); //declare Set of Character type
for(char c : a)
        set.add(c);           //Add chars in 'a' to the set 

System.out.println(set);

OUTPUT : [a, b, c]

1 Comment

Oh, seems the question was edited to say do not use sets when I was typing out the answer. Since its already posted, I think its fine if it remains.
0

What you could do to speedup this is something like, check if the character C is in the outputted characters array A (not the print) using binary search. Print the character C if it's not in the list and then insert (sorted) the character C to A (to be able to use binary search).

Comments

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.