Check out example codes for "Remove duplicate element in an array in java". It will help you in understanding the concepts better.

Code Example 1

remove duplicates from array java 8
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class RemoveDuplicatesJava8
{
   public static void main(String[] args)
   {
      List li1 = new ArrayList<>(Arrays.asList(2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9));
      System.out.println("Before removing duplicate elements: " + li1);
      // create new list from elements of original list
      List li2 = li1.stream().distinct().collect(Collectors.toList());
      // printing ArrayList with duplicates removed
      System.out.println("After removing duplicate elements: " + li2);
   }
}

Code Example 2

remove duplicates from string array java
import java.util.Arrays;
import java.util.LinkedHashSet;
public class DuplicatesFromString
{
   public static void main(String[] args)
   {
      String[] strAnimals = {
              "lion",
              "bear",
              "deer",
              "lion",
              "elephant",
              "deer"
      };
      System.out.println("Before removing duplicates from string array: " + Arrays.toString(strAnimals));
      // convert given string array to list add elements to LinkedHashSet
      // LinkedHashSet will remove duplicate elements
      LinkedHashSet<String> lhsAnimals = new LinkedHashSet<String>(Arrays.asList(strAnimals));
      String[] strNewAnimals = lhsAnimals.toArray(new String[lhsAnimals.size()]);
      System.out.println("After removing duplicates from string array: " + Arrays.toString(strNewAnimals));
   }
}

Code Example 3

public class RemoveDuplicateElementDemo 
{
   public static int removeDuplicate(int[] arrNumbers, int num)
   {  
      if(num == 0 || num == 1)
      {  
         return num;  
      }  
      int[] arrTemporary = new int[num];  
      int b = 0;  
      for(int a = 0; a < num - 1; a++)
      {  
         if(arrNumbers[a] != arrNumbers[a + 1])
         {  
            arrTemporary[b++] = arrNumbers[a];  
         }  
      }  
      arrTemporary[b++] = arrNumbers[num - 1]; 
      for(int a = 0; a < b; a++)
      {  
         arrNumbers[a] = arrTemporary[a];  
      }  
      return b;  
   }
   public static void main(String[] args) 
   {
      int[] arrInput = {1, 2, 3, 3, 4, 5, 5, 6, 7, 8};  
      int len = arrInput.length;  
      len = removeDuplicate(arrInput, len);  
      // printing elements
      for(int a = 0; a < len; a++)
      {
         System.out.print(arrInput[a] + " ");
      }
   }
}

Code Example 4

java program to remove duplicates from arraylist
import java.util.ArrayList;
import java.util.Arrays;
public class DuplicatesFromArraylist
{
   public static <T> ArrayList<T> removeDuplicate(ArrayList<T> al)
   {
      ArrayList<T> alNew = new ArrayList<T>();
      for(T element : al)
      {
         // if element is present in alNew then add it
         if(!alNew.contains(element))
         {
            alNew.add(element);
         }
      }
      // return alNew
      return alNew;
   }
   public static void main(String[] args)
   {
      ArrayList<Integer> al = new ArrayList<>(Arrays.asList(2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9));
      System.out.println("Before removing duplicates from ArrayList: " + al);
      ArrayList<Integer> alNew = removeDuplicate(al);
      // printing ArrayList with duplicates
      System.out.println("After removing duplicates from ArrayList: " + alNew);
   }
}

Code Example 5

remove duplicates from array in java without using collections.
import java.util.Arrays;
public class WithoutUsingCollections
{
   static void removeDuplicateWithoutCollections(int[] arrDuplicate)
   {
      System.out.println("Before removing duplicates from array: ");
      for(int a = 0; a < arrDuplicate.length; a++)
      {
         System.out.print(arrDuplicate[a] + " ");
      }
      int sizeUnique = arrDuplicate.length;
      // compare each element with other element
      for(int a = 0; a < sizeUnique; a++)
      {
         for(int b = a + 1; b < sizeUnique; b++)
         {
            // if any two numbers are equal
            if(arrDuplicate[a] == arrDuplicate[b])
            {
               arrDuplicate[b] = arrDuplicate[sizeUnique - 1];
               sizeUnique--;
               b--;
            }
         }
      }
      int[] arrWithoutDuplicate = Arrays.copyOf(arrDuplicate, sizeUnique);
      // print elements array without duplicate elements
      System.out.println();
      System.out.println("After removing duplicates from array: ");
      for(int a = 0; a < arrWithoutDuplicate.length; a++)
      {
         System.out.print(arrWithoutDuplicate[a] + " ");
      }
      System.out.println();
   }
   public static void main(String[] args)
   {
      removeDuplicateWithoutCollections(new int[] {1, 3, 5, 1, 7, 9});
      removeDuplicateWithoutCollections(new int[] {56, 85, 56, 85, 38, 28});
   }
}

Code Example 6

use Arrays.sort() method
import java.util.Arrays;
public class RemoveDuplicateExample 
{
   public static int removeDuplicate(int[] arrNumbers, int num)
   {  
      if(num == 0 || num == 1)
      {  
         return num;  
      }  
      int[] temp = new int[num];  
      int b = 0;  
      for(int a = 0; a < num - 1; a++)
      {  
         if(arrNumbers[a] != arrNumbers[a + 1])
         {  
            temp[b++] = arrNumbers[a];  
         }  
      }  
      temp[b++] = arrNumbers[num - 1];
      for(int a = 0; a < b; a++)
      {  
         arrNumbers[a] = temp[a];  
      }  
      return b;  
   }
   public static void main(String[] args) 
   {
      int[] arrInput = {2, 12, 10, 6, 8, 8, 2, 14, 12 ,4};
      // first sort array
      Arrays.sort(arrInput);  
      int len = arrInput.length;  
      len = removeDuplicate(arrInput, len);
      for(int a = 0; a < len; a++)
      {
         System.out.print(arrInput[a] + " "); 
      }
   }
}

Code Example 7

remove duplicate element using seperate index.
public class RemoveDuplicateUsingSeperateIndex
{
   public static int removeDuplicate(int[] arrNumbers, int num)
   {
      if(num == 0 || num == 1)
      {
         return num;
      }
      int b = 0;
      for(int a = 0; a < num - 1; a++)
      {
         if(arrNumbers[a] != arrNumbers[a + 1])
         {
            arrNumbers[b++] = arrNumbers[a];
         }
      }
      arrNumbers[b++] = arrNumbers[num - 1];
      return b;
   }
   public static void main(String[] args)
   {
      int[] arrNumbers = {1, 2, 3, 3, 4, 5, 5, 6, 7, 8};
      int len = arrNumbers.length;
      len = removeDuplicate(arrNumbers, len);
      for(int a = 0; a < len; a++)
      {
         System.out.print(arrNumbers[a] + " ");
      }
   }
}

Learn ReactJs, React Native from akashmittal.com