Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để loại bỏ các phần tử trùng lặp khỏi ArrayList

Trong bài này, chúng ta sẽ hiểu cách xóa các phần tử trùng lặp khỏi arrayList. Lớp TheArrayList là một mảng có thể thay đổi kích thước, có thể được tìm thấy trong gói java.util. Sự khác biệt giữa mảng dựng sẵn và ArrayList trong Java là kích thước của mảng không thể được sửa đổi.

Dưới đây là một minh chứng về điều tương tự -

Giả sử đầu vào của chúng tôi là -

Input list : [150, 250, 300, 250, 500, 150, 600, 750, 300]

Đầu ra mong muốn sẽ là -

The list with no duplicates is:
[150, 250, 300, 500, 600, 750]

Thuật toán

Step 1 - START
Step 2 - Declare namely
Step 3 - Define the values.
Step 4 – Create an ArrayList of integer values and initialize elements in it.
Step 5 - Display the ArrayList on the console.
Step 6 - Create another linkedhashset of integers.
Step 7 - Use the ‘addAll’ method to include elements from previous ArrayList into it as elements.
Step 8 - Since it is a set, it only adds the unique values.
Step 9 - Clear the elements of the ArrayList.
Step 10- Display the set on the console with unique elements.
Step 11- Stop

Ví dụ 1

Ở đây, chúng tôi liên kết tất cả các hoạt động với nhau trong hàm "main".

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class Demo {
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      ArrayList<Integer> input_list = new ArrayList<>(Arrays.asList(150, 250, 300, 250, 500, 150, 600, 750, 300));
      System.out.println("The list is defined as: " + input_list);
      Set<Integer> temp_set = new LinkedHashSet<>();
      temp_set.addAll(input_list);
      input_list.clear();
      input_list.addAll(temp_set);
      System.out.println("\nThe list with no duplicates is: \n" + input_list);
   }
}

Đầu ra

The required packages have been imported
The list is defined as: [150, 250, 300, 250, 500, 150, 600, 750, 300]

The list with no duplicates is:
[150, 250, 300, 500, 600, 750]

Ví dụ 2

Ở đây, chúng tôi đóng gói các hoạt động thành các hàm trưng bày lập trình hướng đối tượng.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class Demo {
   static void remove_duplicates(ArrayList<Integer> input_list){
      Set<Integer> temp_set = new LinkedHashSet<>();
      temp_set.addAll(input_list);
      input_list.clear();
      input_list.addAll(temp_set);
      System.out.println("\nThe list with no duplicates is: \n" + input_list);
   }
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      ArrayList<Integer> input_list = new ArrayList<>(Arrays.asList(150, 250, 300, 250, 500, 150, 600, 750, 300));
      System.out.println("The list is defined as: " + input_list);
      remove_duplicates(input_list);
   }
}

Đầu ra

The required packages have been imported
The list is defined as: [150, 250, 300, 250, 500, 150, 600, 750, 300]

The list with no duplicates is:
[150, 250, 300, 500, 600, 750]