Remove duplicate elements in Array

1. Using LinkedHashSet


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Arrays;
import java.util.LinkedHashSet;

public class RemoveDuplicateElementsFromArray {
    public static void main(String[] args) {
        //Array with duplicate elements
        Integer[] numbers = new Integer[] {1,2,3,4,5,1,2,3,4,5};

        //This array has duplicate elements
        System.out.println( Arrays.toString(numbers) );

        //Create set from array elements
        LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>( Arrays.asList(numbers) );

        //Get back the array without duplicates
        Integer[] numbersWithoutDuplicates = linkedHashSet.toArray(new Integer[] {});

        //Verify the array content
        System.out.println( Arrays.toString(numbersWithoutDuplicates) );
    }
}

2. Using Temporary Array

A. If the array is a Sorted Array

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Arrays;

public class RemoveDuplicateElementsFromArray
{
    public static void main(String[] args)
    {
        // Array with duplicate elements
        Integer[] origArray = new Integer[] { 1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 7 };

        // This array has duplicate elements
        System.out.println(Arrays.toString(origArray));

        // Remove Duplicates
        Integer[] tempArray = removeDuplicates(origArray);

        // Verify the array content
        System.out.println(Arrays.toString(tempArray));
    }

    private static Integer[] removeDuplicates(Integer[] originalArray) {

        // Create Array with Same DataType And Length
        Integer[] temporaryArray = new Integer[originalArray.length];


        int indexJ = 0;
        for (int indexI = 0; indexI < originalArray.length - 1; indexI++)
        {
            // get the Current Element Form Original Array
            Integer currentElement = originalArray[indexI];

            // Check if not current element value equals originsl array next value
            if (currentElement != originalArray[indexI+1]) {

                // Check if not current element value equals originsl array next value
                // then add currentElement(OriginalArray) value to Temporary Array
                temporaryArray[indexJ++] = currentElement;
            }
        }

        temporaryArray[indexJ++] = originalArray[originalArray.length-1];

        return temporaryArray;
    }
}

B. If the Array is not Sorted

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Arrays;

public class RemoveDuplicateElementsFromArray
{
    public static void main(String[] args) throws CloneNotSupportedException {
        // Array with duplicate elements
        Integer[] origArray = new Integer[] { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 };

        // This array has duplicate elements
        System.out.println(Arrays.toString(origArray));

        Integer[] tempArray = removeDuplicates(origArray);

        // Verify the array content
        System.out.println(Arrays.toString(tempArray));
    }

    private static Integer[] removeDuplicates(Integer[] origArray) {

        for (int j = 0; j < origArray.length - 1; j++) {
            for (int i = j + 1; i < origArray.length - 1; i++) {
                if (origArray[j] == origArray[i]) {
                    origArray[i] = null;
                }
            }
        }

        origArray[origArray.length - 1] = null;

        return origArray;
    }
}

Comments

Popular Posts