Please I need a java expert to help me with the code and algorithm that deletes duplicate element from an array.
I have written a programm to do this as shown below using my own algpritm. Though it is working but I think my code is too long for this exercise. I am a beginner in Java. see code below;

Inline Code Example Here

public class ArrayDuplicateRemoval {

        int [] rawArray = {1,2,3,1,2,3,4,4,2,1,2,1,5,1};
        int [] newArray = new int [rawArray.length];
        int countA = 0;
        int countB = 0;
        public static void main(String[] args) {
          ArrayDuplicateRemoval dup = new ArrayDuplicateRemoval();
          dup.feedArray();
          dup.displayArray();
        }
      public void feedArray(){
      if(countA < newArray.length){
          removeDuplicate(rawArray[countB]);
      }
      }   
    public void removeDuplicate(int arrayElement){
    for (int x:newArray)
             if (arrayElement == x)
          {
            countA++;
            countB++;
            feedArray();  
         }   
    for (int m:newArray) 

        if ((m != arrayElement )&(countA < newArray.length))
        {
        newArray [countA] = arrayElement;
        countA++;
        countB++;
        feedArray();
        }     
    }

    public void displayArray(){
    System.out.println("Values in the raw array are listed below");

    for(int k: rawArray)
    System.out.printf(" %d", k);
     System.out.println();
         System.out.println("\nValues in the array after duplicate numbers are removed");

       for(int c: newArray)
       {
        if( c != 0)
       System.out.printf(" %d", c);
       }
       System.out.println();
    }

    }



    Please help me with the shortest possible algorith that will perform the same function.
    Thank you in advance.

What if you use a data structure called Set, it doesn't hold duplicate elements

a next problem, is that you'll have a number of '0' 's in there. and who is to say in the original array there wasn't a 0?

@ slavi, I don't want to use any data structure or array method this time. I want to design the algorithm myself.

@ stultuske, yes after removing the duplicate numbers, 0 takes the position of the elements but while printing the array, I eliminated all the 0s.

Please if possible, let me see your solution using your own algorithm.

Zubenna: you were already given a proper suggestion: use a Set.
you don't want to use any data structure ... an array IS a data structure.
as for the 'I have removed the 0's, what if your original array was:

{1,5,0,5,3,0,1,8}

? do you now see your problem?

I stil don't understand your reasoning behind not using a set, will be 1 line solution. Anyhow, this should work(pseudo):

removeDuplicates(int[] arrayNumbers)
    int[] result
    for number in arrayNumbers
        if number not in result: append(number) to result
    return result

slavi: he would still need to keep a counter to check how many elements were added, and add another array after returning the result. otherwise, either he needs to make a new array each time he adds a number, or he ends up with an array that has trailing 0 elements.

commented: fair point, didn't really think of adding new numbers by the description he has +6

I sense a non-argument here! If the goal is to write the simplest Java code then use a set. If this is a beginners exercise in arrays and algorithms then Slavi's algorithm with stultuske's addendum. My guess is that it's the beginner's version.

Thank you all for your contributions. I am only trying to solve an exercise. I know that set can handle it, but the exercise was to write a programm to remove duplicate in an array without using any other data structure.
@ slavi, I have modified the programme to work fine even with 0 elements. I am a biginner trying to develop my skills in Java. It is my passion. Please keep on assisting me as much as you can. Advice me also on what to do to become a java expert like you guys.
Thanks.

use hash map in java it allows only unique records,,,

Ambrish
Read the whole thread before posting. That way you won't look foolish when you post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.