Remove duplicate element in an Array and List in java

Program to remove duplicate element in an Array using Temp Array

class RemoveDuplicateInArray {
  public static int removeDuplicateElementsFromArray(int array[], int size) {
    if (size == 0 || size == 1) {
      return size;
    }
    int[] tempArray = new int[size];
    int j = 0;
    for (int i = 0; i < size - 1; i++) {
      if (array[i] != array[i + 1]) {
        tempArray[j++] = array[i];
      }
    }
    tempArray[j++] = array[size - 1];
    // Changing original array
    for (int i = 0; i < j; i++) {
      array[i] = tempArray[i];
    }
    return j;
  }
}

class RemoveDuplicateCall {

  public static void main(String[] args) {
    int originalArray[] = { 15, 25, 25, 35, 35, 60, 70, 70, 80 };
    int size = originalArray.length;
    size = RemoveDuplicateInArray.removeDuplicateElementsFromArray(originalArray, size);
    for (int i = 0; i < size; i++)
      System.out.print(originalArray[i] + " ");
  }
}

Output

15 25 35 60 70 80

Program to remove duplicate element in an Array using separate index

public class RemoveDuplicateInArray {
  public static int removeDuplicateElements(int originalArray[], int size) {
    if (size == 0 || size == 1) {
      return size;
    }
    int j = 0;
    for (int i = 0; i < size - 1; i++) {
      if (originalArray[i] != originalArray[i + 1]) {
        originalArray[j++] = originalArray[i];
      }
    }
    originalArray[j++] = originalArray[size - 1];
    return j;
  }

  public static void main(String[] args) {
    int originalArray[] = { 15, 25, 25, 35, 35, 60, 70, 70, 80 };
    int size = originalArray.length;
    size = removeDuplicateElements(originalArray, size);

    for (int i = 0; i < size; i++)
      System.out.print(originalArray[i] + " ");
  }
}

Output

15 25 35 60 70 80

Program to removed duplicates from List

import java.util.*;

public class RemoveDuplicateInArray {

  public static void main(String agrs[]) {

    List<Integer> list = new ArrayList<Integer>();
    list.add(12);
    list.add(12);
    list.add(22);
    list.add(30);
    list.add(44);
    list.add(44);
    list.add(111);
    list.add(111);
    list.add(111);

    System.out.println("Original List "+list);
    LinkedHashSet<Integer> set = new LinkedHashSet<Integer>(list);
  // LinkedHashSet maintain insertion order
                                                                 
    list = new ArrayList<Integer>();

    list.addAll(set);

    System.out.println("List After remove duplicates "+list);

  }

}

Output

Original List [12, 12, 22, 30, 44, 44, 111, 111, 111]
List After remove duplicates [12, 22, 30, 44, 111]