Hey Guys Im working on an assignment dealing with arrays. I have to Delete the duplicates in the array and re-display the array with the duplicates deleted. I Have code that I written up that is able to step throgh the process and will display the duplicated elements in the array. I woas wondering if i was missing a step or i was missing another for loop or how would i go about doing this. Havent coded java in a few years, but any ways here is the no dups code:

public void NOdups()
  {
                                                    //Declare a constant long value as the "null" value
        for (int j = 0; j<nElems; j++)          //Declare outer and inner loop indices
       {                                            //For each element starting at the
                                                      beginning of the array and up to
                                                      the 2nd-last-of-element
           for(int k = j+1; k<nElems; k++)          //For each element starting at the
                                                      beginning of the array and 
                                                      up to the 2nd-last-of-element
           {
               if(j == k)                           //If the element at the outer index = the element at the innner index
               {
                   break;
               }
               else if (a[k] == a[j])               //The element at the inner index gets the "null" value
               {
                nElems--;
               }
           }//end k loop
       }//end j loop
   } //end NOdups

and then here is the is the class:

class HighArrayApp
   {
   public static void main(String[] args)
      {
      int maxSize = 100;            // array size
      HighArray arr;                // reference to array
      arr = new HighArray(maxSize); // create the array

      arr.insert(17);               // insert 10 items
      arr.insert(99);
      arr.insert(44);
      arr.insert(55);
      arr.insert(17);
      arr.insert(17);
      arr.insert(11);
      arr.insert(00);
      arr.insert(66);
      arr.insert(33);

      arr.getMax();                 //displays Max Number of the Array
      arr.display();                // display items

      int searchKey = 35;           // search for item
      if( arr.find(searchKey) )
         System.out.println("Found " + searchKey);
      else
         System.out.println("Can't find " + searchKey);

      arr.delete(00);               // delete 3 items
      arr.delete(55);
      arr.delete(99);

      arr.getMax();                 //Displays max Number after deleting 3 arrays
      arr.display();                

      arr.NOdups();
      arr.display();
      }  // end main()
   }  // end class HighArrayApp

When the Project is run i get this output:

Max Number in Current Array: 99
17 99 44 55 17 17 11 0 66 33
Can't find 35
Max Number in Current Array: 66
17 44 17 17 11 66 33
17 44 17 17
BUILD SUCCESSFUL (total time: 0 seconds)

(Clarification:)I need it to not display the duplicates but display the left over array and I have the Display() method all set
would really appreciate the help

I assume HighArray is some kind of ArrayList-like object, so all you need is around line 18 to delete the duplicate you just found. Remember that you will need to update k to allow for that deletion otherwize you won't detect consecutive duplicates.

ps line 12 etc is redundant because k starts > j and just gets bigger!

ok so this is what i have done I have deleted j and now i need to update k, could you explain a bit more not sur is i understand? thank you for the help

What ive got now:

   public void NOdups()
  {
      for (int j = 0; j<nElems; j++)     
      {                             
           for(int k = j+1; k<nElems; k++)   
           {
               if(a[j] == a[k])                           
               {
                   delete (j);
                   nElems--;
               }
           }//end k loop
       }//end j loop
   } //end NOdups

If you delete an entry (i) then the next entry will become the new entry (i) so you will need to look at that next time round the loop, so you will need to reset that loop variable back one. YOu may find deleteing (k) has simpler side effects than deleting (j) for that reason.
Gotta go now.
J

Alright sweet Thank you had a very long weekend. Finally got it to work! here is my final code layout!

 public void NOdups()
  {
        for (int j = 0; j<nElems; j++)              
       {                                           
           for(int k = j + 1; k<nElems; k++)
           {
               if(a[j] == a[k])
                 delete(a[k]);
           }//end k loop
       }//end j loop
   } //end NOdups

I think you may still have a bug in there (depending on how exactly the HighArray class works). Try a very simple test case {1,1,1} That should end up as just a single {1}, but I think you may end up with two {1,1}

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.