Q:

how to remove duplicates from an array java

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] + " ");
      }
   }
}
1
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);
   }
}
1
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] + " "); 
      }
   }
}
1
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] + " ");
      }
   }
}
1
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));
   }
}
1
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});
   }
}
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);
   }
}
1
import java.util.*;

public class RemoveDuplicatesFromArrayList {

    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1,2,2,2,3,5);

        System.out.println(numbers);

        Set<Integer> hashSet = new LinkedHashSet(numbers);
        ArrayList<Integer> removedDuplicates = new ArrayList(hashSet);

        System.out.println(removedDuplicates);
    }
}
1
// Must sort arrays first --> Arrays.sort(arrayName)
public class RemoveDuplicateInArrayExample{  
public static int removeDuplicateElements(int arr[], int n){  
        if (n==0 || n==1){  
            return n;  
        }  
        int[] temp = new int[n];  
        int j = 0;  
        for (int i=0; i<n-1; i++){  
            if (arr[i] != arr[i+1]){  
                temp[j++] = arr[i];  
            }  
         }  
        temp[j++] = arr[n-1];     
        // Changing original array  
        for (int i=0; i<j; i++){  
            arr[i] = temp[i];  
        }  
        return j;  
    }  
       
    public static void main (String[] args) {  
        int arr[] = {10,20,20,30,30,40,50,50};  
        int length = arr.length;  
        length = removeDuplicateElements(arr, length);  
        //printing array elements  
        for (int i=0; i<length; i++)  
           System.out.print(arr[i]+" ");  
    }  
} 
2
public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list){
  Set<T> set = new LinkedHashSet<>(list);
  return new ArrayList<T>(set);
}
0

New to Communities?

Join the community