0
public static void main(String[] args) 
{

    char[] x = {'b', 'l', 'a', 'h', 'h', ' '};
    char[] y = {'g', 'o', 'g', 'o'};

    System.out.println(removeDuplicate(x, y));
    System.out.println(noDuplicate(y, x));

public static char[] removeDuplicate(char[] first, char[] second)
{  

   //used my append method (didn't enclose) to append the two words together
   char[] total1 = append(first, second);

   //stores character that have been encountered
   char[] norepeat = new char[total1.length];

   int index = 0;

   //store result
   char[] solution = new char[total1.length];

   boolean found = false;

   //for loop keeps running until blahh gogo is over
   for(int i = 0; i < total1.length; i++)
   {
       for(int m = 0; m <norepeat.length; m++)
       {
            if(total1[i] == norepeat[m])
            {  
                found = true;
                break;
            }
   }

    if (!found)
       {   
           norepeat[index] = total1[i];
           index++;

           solution[index] = total1[i];
           index++;
       }
   }

   return solution;
}
}

Current Output:

blah

go

I Want the Output to be:

blah go

goblah (space at the end)

The problem with my code is that it stops running after encountering the first repeat, so it doesn't even run the whole words at all. I believe that it has something to do with my nested for loop, but I am not sure. I tried writing it out on paper, but it doesn't seem to help any.

Any help would be appreciated! Thank you!

Chirag Parmar
  • 833
  • 11
  • 26
  • What is the logic by which you end up with `goblah` in your output? – Tim Biegeleisen Nov 02 '16 at 06:47
  • Why you are not using hashSet ?? Use HashSet problem will be solved in O(N) time. Dont write code which is already written. – Nikesh Joshi Nov 02 '16 at 06:48
  • @TimBiegeleisen In main when I printed out "System.out.println(noDuplicate(y, x));" I am now reading the char[] from y to x -- therefore read from gogo to blahh . Therefore for what I want the second output to be is goblah after the duplicates has been removed. Hope I clarified that for you. – sushiprograms Nov 02 '16 at 07:07
  • @nikeshjoshi I am a beginner java programmer, am just learning my basics right now. I have yet to learn about HashSet... – sushiprograms Nov 02 '16 at 07:08
  • If your current output only outputs value of `first` (which is what seems to happen), perhaps your `append()` method doesn't work. --- What is the difference between `norepeat` and `solution`? I mean, other than `norepeat` gets all the even indexes assigned and `solution` gets all the odd indexes assigned, because you increment `index` twice in the `if (!found)` block. --- Maybe you should **debug** your code. See [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/q/25385173/5221149) – Andreas Nov 02 '16 at 07:36

4 Answers4

0

I think that you need to assign the found variable as false in the beginning of the first loop. After your first turn the value of found is always true.

for(int i=0;i<total1.length;i++)
{
 found=false;
 for(int m = 0; m <norepeat.length; m++)
   {      
        if(total1[i] == norepeat[m])
        {  
            found = true;
            break;
   }

You can also use set of characters instead of using char array.A set is a collection which doesn't allow duplication of elements.For more info. about sets you can visit this link.Sets

Ankit
  • 51
  • 6
0

If the possible letters is the ASCII range, then you can use a simple boolean array to keep track of letters you've seen already:

boolean[] seen = new boolean[256];

If you don't modifying the original array of characters, then you can arrange unique elements to the beginning of the array, and then create a new array of the first size elements and return it.

int size = 0;
for (int j = 0; j < chars.length; j++) {
    char c = chars[j];
    if (!seen[c]) {
        chars[size++] = chars[j];
        seen[c] = true;
    }
}
return Arrays.copyOf(chars, size);

If the alphabet can be more than the ASCII range, you can use a Set<Character> to track characters seen.

janos
  • 120,954
  • 29
  • 226
  • 236
0

Here is my solution using Map and List as helpers. Input and output as char arrays are preserved.

Note that you should not init solution at the length of total1 otherwise the char array will print blanks at the end.

public static char[] removeDuplicate(char[] first, char[] second){  

   //used my append method (didn't enclose) to append the two words together
   char[] total1 = append(first, second);

   //stores character that have been encountered
   Map<Character,Boolean> norepeat = new HashMap<Character,Boolean>();
   //store partial result
   List<Character> partSolution=new ArrayList<Character>();

    //for loop keeps running until blahh gogo is over
    for(int i = 0; i < total1.length; i++){
        if(! norepeat.containsKey(total1[i])) {   
            norepeat.put(total1[i], true) ;
            partSolution.add(total1[i]);
        }

    }

    //store final result
    char[] solution = new char[partSolution.size()];
    for(int i=0;i<partSolution.size();i++){
       solution[i]=partSolution.get(i);
    }    

    return solution;

}

Massimo Petrus
  • 1,881
  • 2
  • 13
  • 26
-1

If you need to get rid of duplicate elements and to preserve the order of array you may use LinkedHashSet. Here is an example:

Set<Character> x = new LinkedHashSet<Character>(Arrays.asList(new Character[]{'b', 'l', 'a', 'h', 'h', ' '}));
Set<Character> y = new LinkedHashSet<Character>(Arrays.asList(new Character[]{'g', 'o', 'g', 'o'}));

System.out.println(x);
System.out.println(y);

Output:

[b, l, a, h,  ]
[g, o]

Edit. Here is an example how you may convert char[] to Character[] and vice versa:

char[] a = new char[]{'b', 'l', 'a', 'h', 'h', ' '};
Character[] boxed = IntStream.range(0, a.length).mapToObj(i -> a[i]).toArray(size -> new Character[size]);
char[] unboxed = IntStream.range(0, boxed.length).mapToObj(i -> String.valueOf(boxed[i])).collect(Collectors.joining()).toCharArray();
eparvan
  • 1,639
  • 1
  • 15
  • 26