Java >> Tutoriel Java >  >> Java

Supprimer les doublons du tableau en JAVA

Dans ce didacticiel, vous apprendrez à supprimer les doublons d'un tableau en Java en utilisant la méthode suivante :

  • Supprimer les doublons du tableau trié
  • Supprimer les doublons du tableau non trié
  • Utilisation de la classe Java LinkedHashSet

Supprimer les doublons du tableau trié

Exemple :

//Java program to remove duplicate elements from array
//From Sorted Array

//Main Class of the program
public class Main{
//Custom Function
public static int removeDuplicateElementsFromArray(int array[], int arrayLength){
        //Checking the length of the passed array
        if (arrayLength==0 || arrayLength==1){
            return arrayLength;
        }
        //Creating a temporary array of the passed array lenght
        int[] temp = new int[arrayLength];
        int j = 0;
        //For Loop itteration
        for (int i=0; i<arrayLength-1; i++){
            if (array[i] != array[i+1]){
                temp[j++] = array[i];
            }
         }
        //Setting the last element of array to the temp
        temp[j++] = array[arrayLength-1];
        // Changing original array
        for (int i=0; i<j; i++){
            array[i] = temp[i];
        }
        return j;
    }

    //Main method of the program
    public static void main (String[] args) {
        //Declaring and Initilizing of arrray
        int arr[] = {10,10,20,20,20,30,30,40,40,40,50,50,50};
        //Get the lenght of the array
        int n = arr.length;
        //Calling the custom function to remove duplicate elements
        n = removeDuplicateElementsFromArray(arr, n);
        //printing array elements
        for (int i=0; i<n; i++)
           System.out.print(arr[i]+" ");
    }
}

Sortie :

10 20 30 40 50 

Supprimer les doublons du tableau non trié

Exemple :

//Java program to remove duplicate elements from array
//From Unsorted Array

//Importing the Array from the util package of java
import java.util.Arrays;

//Main Class of the program
public class Main{
//Custom Function
public static int removeDuplicateElementsFromArray(int array[], int arrayLength){
        //Checking the length of the passed array
        if (arrayLength==0 || arrayLength==1){
            return arrayLength;
        }
        //Creating a temporary array of the passed array lenght
        int[] temp = new int[arrayLength];
        int j = 0;
        //For Loop itteration
        for (int i=0; i<arrayLength-1; i++){
            if (array[i] != array[i+1]){
                temp[j++] = array[i];
            }
         }
        //Setting the last element of array to the temp
        temp[j++] = array[arrayLength-1];
        // Changing original array
        for (int i=0; i<j; i++){
            array[i] = temp[i];
        }
        return j;
    }

    //Main method of the program
    public static void main (String[] args) {
        //Declaring and Initilizing of arrray
        int arr[] = {10,20,8,57,23,23,8,10,57,20};
        //sort function of array to sort the elements
        Arrays.sort(arr);
        //Get the lenght of the array
        int n = arr.length;
        //Calling the custom function to remove duplicate elements
        n = removeDuplicateElementsFromArray(arr, n);
        //printing array elements
        for (int i=0; i<n; i++)
           System.out.print(arr[i]+" ");
    }
} 

Sortie :

8 10 20 23 57 

Utilisation de la classe Java LinkedHashSet

Exemple :

//Java code to remove duplicate from array

//Using Java LinkedHashSet class

//Importing the Util Package
import java.util.*;

//Main Class of program
public class Main
{
    //Main method of the program
    public static void main(String[] args) throws CloneNotSupportedException{
        //Variable to take number of elements in Array
        int n;
        //Creating object for Scanner Class
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the Number of elements in array : ");
        //Taking input from user
        n = input.nextInt();
        //Creating the Integer type array variable of `n` size
        Integer[] arr = new Integer[n];
        //For loop for iteration
        for(int i = 0; i < n; i++){
            //Taking input on every iteration for `i` th location
            arr[i] = input.nextInt();
        }
        //Printing Duplicate array
        System.out.println("With Duplicate Values : "+Arrays.toString(arr));
        //Creating Set from array
        LinkedHashSet<Integer> hashTable = new LinkedHashSet<>( Arrays.asList(arr) );
        //printing the output
        System.out.println("Without Duplicate Values : "+hashTable.toString());
    }
}

Sortie :

Enter the Number of elements in array : 3
1
22
22
With Duplicate Values : [1, 22, 22]
Without Duplicate Values : [1, 22] 


Balise Java