1

I am using first array to store all database of numbers where some numbers are duplicates.

I have went through this array to see which items are duplicated and am adding index of duplicated items to second array.

Now, I must loop through first array and add all but duplicated values to third array (assuming that we know which fields are duplicated).

But how to do this correctly? I can't make it stop adding every item from first array to third array.

Assuming I can't use HashSet().

The purpose of this is to demonstrate how to move one array to other with removed duplicated in O(N) time complexity.

Input numbers: 00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99
Output which index are duplicated: 1, 2, 6, 7
Output I get: 00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99 (same as the input)

Code:

   public void dups() 
   {
       int[] b = new int[100];
       int[] c = new int[100];

       int k = 0;
       int n = 0;
       int p = 0;

       for (int i = 0; i < nElems; i++)
           for (int j = 0; j < nElems; j++)
               if(a[j].equals(a[i]) && j != i)
                   b[k++] = i;

       for (int l = 0; l < k; l++)
           System.out.print(b[l] + " ");

       for (int m = 0; m < nElems; m++)
           if (m != b[p + 2])
               c[m] = (Integer) a[n++];

       System.out.print("\n");

       for (int o = 0; o < nElems; o++)
           System.out.print(c[o] + " ");
   }
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155

5 Answers5

3

It can be done in a simpler way:

Set<Integer> uniqueSet = new HashSet<Integer>();
uniqueSet.addAll(list);
//not uniqueSet contains only unique elements from the list.

The reason it works is that a Set cannot contain duplicates. So while adding elements to a Set, it ignores those that are duplicates.

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
3

You can use a HashSet instead of Array to store all database of numbers. After that use Collections.toArray() to get your desired Array.

I see that the question got edited and we don't want to use HashSet anymore. Anyways, your problem is already answered here, Algorithm: efficient way to remove duplicate integers from an array

Community
  • 1
  • 1
Arham
  • 2,072
  • 2
  • 18
  • 30
1

Instead of marking all duplicates you could mark all that have already been seen earlier.

Instead of:

for (int i = 0; i < nElems; i++)
  for (int j = 0; j < nElems; j++)
    if(a[j].equals(a[i]) && j != i)
      b[k++] = i;

Use something like:

for (int i = 0; i < nElems; i++)
  for (int j = i+1; j < nElems; j++)
    if(a[j].equals(a[i]))
      b[k++] = j;

You should then see:

Output which index are duplicated: 2, 7

Which should be much easier to work with.

Here's a working solution - although I wouldn't do it this way:

public class Test {
  Integer[] a = {00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99};
  int nElems = a.length;

  public void dups() {
    int[] b = new int[100];
    int[] c = new int[100];

    int k = 0;
    int n = 0;
    int p = 0;

    for (int i = 0; i < nElems; i++) {
      for (int j = i + 1; j < nElems; j++) {
        if (a[j].equals(a[i])) {
          b[k++] = j;
        }
      }
    }

    for (int l = 0; l < k; l++) {
      System.out.print(b[l] + " ");
    }
    for (int m = 0; m < nElems; m++) {
      if (m != b[p]) {
        c[n++] = a[m];
      } else {
        p += 1;
      }
    }

    System.out.print("\n");

    for (int o = 0; o < nElems - k; o++) {
      System.out.print(c[o] + " ");
    }
  }

  public static void main(String args[]) {
    new Test().dups();
  }
}

which prints:

2 7 
0 11 22 33 44 55 66 77 88 99
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • This is exactly what I was trying to accomplish! You even solved my problem with double duplicate index in array b[]. Thank you so much :) – HelpNeeder Oct 07 '12 at 22:51
  • Thanks! Please be sure you understand how I did it and how using a `HashSet` would have been so much better. In particular why `o < nElems - k` is correct for the final loop. – OldCurmudgeon Oct 07 '12 at 23:10
  • I agree, I'm taking Java class and I think professor tried to demonstrated how to build that from scratch. I understand that nElems is the number of entries in the array (suppose that we don't have an access to length() method). When we remove 2 items, the same amount of numbers should be removed from the original array. – HelpNeeder Oct 07 '12 at 23:18
0

I don't know if this is what you were looking for. But it removes duplicate. Give it a shot,

int[] b = { 00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99 };
    List<Integer> noDups = new ArrayList<Integer>();

    Boolean dupliceExists;
    for (int i = 0; i < b.length; i++) {
        dupliceExists = Boolean.FALSE;
        for (Integer integ : noDups) {
            if (Integer.valueOf(b[i]).equals(integ)) {
                dupliceExists = Boolean.TRUE;
                              //Get index if you need the index of duplicate from here
            }
        }
        if (!dupliceExists) {
            noDups.add(b[i]);
        }
    }

    for (int i = 0; i < noDups.size(); i++) {
        System.out.println(noDups.get(i));
    }
Jimmy
  • 2,589
  • 21
  • 31
  • You should try to to use the default helper methods first rather than re-inventing the wheel. Like in your code, you can simply omit the logic related to 'dupliceExists' and place the following condition to control the duplicates:- if(!noDups.contains(Integer.valueOf(b[i]))) {noDups.add(b[i])} – Arham Oct 07 '12 at 20:11
0

I am going to coding for copying non-duplicate elements from one Array to another Array.

/*
 * print number of occurance of a number in an array beside it
 * 
 * */
class SpoorNumberOfOccuranceInArray{
  public static void main(String[] args){
    int[] arr={65,30,30,65,65,70,70,70,80};    
    int[] tempArr=new int[arr.length];//making size compatible to source Array. 
    int count=0;       
    for(int i=0;i<arr.length;i++){
      int temp=arr[i];
      for(int j=0;j<tempArr.length;j++){
        if(temp==tempArr[j]){   //Comparing the Availability of duplicate elements in the second Array.---------       
           break;           
        }        
        else{    //Inserting if value is not in the second Array.---------------               
          if( tempArr[j]==0){             
             tempArr[j]=temp;
              break;
            }//end if         
        }//end else
      }//end if    
    }//end for
    for(int i=0;i<tempArr.length;i++){
      for(int j=0;j<arr.length;j++){
        if(tempArr[i]==arr[j])
         count++;
      }
      System.out.println(tempArr[i]+" "+count); 
      count=0;
    }   
  }
}