Vicky
Vicky

Reputation: 1

Need to find and store the duplicate numbers in a array in another array but dont know how to put the duplicate's in another array

I have found out the duplicate numbers in the array "a" but could not store the duplicate ones in another array "b". Any help much appreciated!

Here is my code:

public static void main(String[] args) {
    int[] a = {1,2,3,3,5,6,1,7,7};
    int[] b={};


    for (int i = 0; i < a.length; i++) {
        for (int j = i+1; j < a.length; j++) {
            if(a[i]==(a[j])){
                System.out.println(a[j]);

            }

        }
    }       

Upvotes: 0

Views: 1650

Answers (3)

codeLover
codeLover

Reputation: 2592

You can try finding duplicates in an array using collections framework too:

import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;

public class MyClass {
    public static void main(String args[]) {
        int[] a = {1,2,3,3,5,6,1,7,7};
        Set<Integer> set=new HashSet<Integer>();
        List<Integer> list=new ArrayList<Integer>();
        for(int i:a)    {
            if(!set.add(i))   {
                list.add(i);
            }
        }
        int[] b=new int[list.size()];
        for (int i=0;i<list.size();i++)    {
            b[i] =list.get(i);
        }

    }
}

Here I have used the fact that sets do not allow duplicate and if set.add returns false, that means that element is already available in the set

Upvotes: 1

vinS
vinS

Reputation: 1475

We cannot change the size of Array once it is initialized. Here we do not know how many duplicates item will be in the Array.

EDIT -- This might be a better solution, my earlier solution was throwing ArrayIndexOutOfBoundException as indicated by @YCF_L --

import java.util.Arrays;

public class T {

    public static void main(String[] args) {

        //int[] a = {1, 2, 3, 3, 5, 6, 1, 7, 7}; //Set-1, provided by @YCF_L
        int[] a = {1,1,3,3,5,6,1,7,7}; //Set-2, provided by OP
        int[] b = {};

        for (int i=0, k=0; i < a.length; i++) {
            for (int j = i+1; j < a.length; j++) {
                if(a[i] == a[j]){
                    System.out.println(a[j]);
                    k = k+1;
                    int[] p  = new int[k];
                    for(int x=0; x < b.length; x++) {
                        p[x] = b[x];
                    }
                    p[k-1] = a[i];
                    b = p;
                }
            }
        }
        System.out.println(Arrays.toString(b));
        System.out.println(Arrays.toString(removeDuplicates(b)));
    }

    public static int[] removeDuplicates(int[] a) {
        boolean[] bArr = new boolean[1000];
        int j = 0;
        for (int i = 0; i < a.length; i++) {
            if (!bArr[a[i]]) {
                bArr[a[i]] = true;
                j++;
            }
        }
        int[] b = new int[j];
        int c = 0;
        for (int i = 0; i < bArr.length; i++) {
            if (bArr[i]) {
                b[c++] = i;
            }
        }
        return b;
    }
}

Output with Set-1 :

[1, 3, 7]
[1, 3, 7]

Output with Set-2 :

[1, 1, 1, 3, 7]
[1, 3, 7]

Upvotes: 0

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59968

Because the length of result is not know, I would like to use a ArrayList instead of an array :

int[] a = {1, 2, 3, 3, 5, 6, 1, 7, 7};
List<Integer> b = new ArrayList<>();
for (int i = 0; i < a.length; i++) {
    for (int j = i + 1; j < a.length; j++) {
        if (a[i] == a[j] && !b.contains(a[j])) {
            b.add(a[j]);
            System.out.println(a[j]);
        }
    }
}
System.out.println(b);//result : [1, 3, 7]

Upvotes: 2

Related Questions