0

Please look in to the below code. It print the only duplicate elements in array. I need to print also rest of array element, please try to help me.

public class DuplicateArray {
    public static void main(String args[]) {
        int array[] = { 10, 20, 30, 20, 40, 40, 50, 60, 70, 80 };// array of ten elements
        int size = array.length;
        System.out.println("Size before deletion: " + size);

        for (int i = 0; i < size; i++) {
            for (int j = i + 1; j < size; j++) {
                if ((array[i]== array[j])) { // checking one element with all the element
                   System.out.println(array[i]);
                }   
            }   
        }

Ouput:

20
40

Here I need to print the rest of array element ?

4castle
  • 32,613
  • 11
  • 69
  • 106
  • Define "rest of array element". Are you trying to print the rest of the array after the final duplicate element? – Cricket Aug 18 '16 at 17:36
  • Please give your desired output. – 4castle Aug 18 '16 at 17:36
  • " rest of array element" Do you mean the non-duplicate elements? – bradimus Aug 18 '16 at 17:38
  • possible duplicate: http://stackoverflow.com/questions/17967114/how-to-efficiently-remove-duplicates-from-an-array-without-using-set – Shahid Aug 18 '16 at 17:40
  • Note that if "20" appears again (a third instance), your code will print "20" multiple times. For example, if your array is ` { 10, 20, 30, 20, 40, 40, 50, 20, 70, 80 };` the output will be `20 20 20 40`. Is this what you want, or do you want "20" to just appear once in the output? – FredK Aug 18 '16 at 17:40

3 Answers3

2

In Java 8, you can easily remove duplicates from an integer array using IntStream.

int[] noDuplicates = IntStream.of(array).distinct().toArray();

To print them instead of putting them in an array, use

IntStream.of(array).distinct().forEach(System.out::println);
4castle
  • 32,613
  • 11
  • 69
  • 106
0

You can use Set to solve this.

List<Integer> list = Arrays.asList(ArrayUtils.toObject(array));
Set<Integer> set = new HashSet<Integer>(list);
List<Integer> withoutDuplicates = new ArrayList<Integer>(set);
int[] newArrayWithoutDuplicates = ArrayUtils.toPrimitive(withoutDuplicates.toArray(new int[withoutDuplicates.size()]));

Finally, you have the array without duplicates.

4castle
  • 32,613
  • 11
  • 69
  • 106
Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
0

Your question as to what you want to do isn't very clear but what I think is you want is to print the array elements without repetition.

If this is indeed the case, the following code will do it.(The explanation of the code is given after the output)

public class DuplicateArray {
public static void main(String args[]) {
    int array[] = { 10, 20, 30, 20, 40, 40, 50, 60, 70, 80 };// array of ten elements
    int size = array.length;
    System.out.println("Size before deletion: " + size);

    boolean duplicateElement=false;//this becomes true if there is a duplicate element in the array before the occurrence of this element
    for (int i = 0; i < size; i++, duplicateElement=false) {
        for (int j = 0; j < i; j++) {
            if ((array[i]== array[j])) { // checking one element with all the elements
               duplicateElement=true; //Duplicate element found in array
               break;
            }   
        }
        if(!duplicateElement)
            System.out.println(array[i]);
       }
}
}  

Output:

10
20
30
40
50
60
70
80

Now, the idea in this code is instead of starting the inner loop from i+1 and going till array.size, start it from 0 and go till i. duplicateElement is a flag variable the value of which becomes true if a duplicate element of array[i] is present in the array at position < i. However, when it's the first occurrence of the duplicate element, there is no other same element before array[i], only after. Hence the repetitive elements are also printed just once

4castle
  • 32,613
  • 11
  • 69
  • 106
Mohd Bilal
  • 46
  • 3