Home > Java > javaTutorial > body text

Detailed explanation of five classic Java array deduplication algorithms

WBOY
Release: 2023-12-23 10:01:19
Original
835 people have browsed it

Detailed explanation of five classic Java array deduplication algorithms

Detailed explanation of five classic Java array deduplication algorithms

In Java programming, we often encounter situations where we need to perform deduplication operations on arrays, that is, remove arrays Duplicate elements in , retain unique elements. The following will introduce five classic Java array deduplication algorithms and provide corresponding code examples.

  1. Using HashSet
    HashSet is a collection class in Java that automatically removes duplicate elements. This feature can be used to quickly achieve array deduplication.
    Code example:
import java.util.Arrays;
import java.util.HashSet;

public class ArrayDeduplicateExample {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 2, 4, 3, 5};
        
        HashSet<Integer> set = new HashSet<>();
        for (int i : array) {
            set.add(i);
        }
        
        int[] result = new int[set.size()];
        int index = 0;
        for (int i : set) {
            result[index++] = i;
        }
        
        System.out.println(Arrays.toString(result));
    }
}
Copy after login
  1. Using TreeSet
    TreeSet is an ordered collection class in Java that automatically removes duplicates and sorts elements. Array deduplication can be achieved by adding elements in the array to a TreeSet and then converting the TreeSet into an array.
    Code example:
import java.util.Arrays;
import java.util.TreeSet;

public class ArrayDeduplicateExample {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 2, 4, 3, 5};
        
        TreeSet<Integer> set = new TreeSet<>();
        for (int i : array) {
            set.add(i);
        }
        
        Integer[] result = set.toArray(new Integer[0]);
        int[] deduplicatedArray = Arrays.stream(result).mapToInt(Integer::intValue).toArray();
        
        System.out.println(Arrays.toString(deduplicatedArray));
    }
}
Copy after login
  1. Using the Stream API
    The Stream API introduced in Java 8 and above can simplify array deduplication operations. By converting the array to a stream, use the distinct method of the stream to remove duplicate elements, and then convert it to an array.
    Code example:
import java.util.Arrays;

public class ArrayDeduplicateExample {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 2, 4, 3, 5};
        
        int[] deduplicatedArray = Arrays.stream(array).distinct().toArray();
        
        System.out.println(Arrays.toString(deduplicatedArray));
    }
}
Copy after login
  1. Using double loop
    Double loop is a common array deduplication algorithm. By comparing adjacent elements, duplicate elements are set to a specified illegal values, and then perform the illegal value removal operation.
    Code example:
import java.util.Arrays;

public class ArrayDeduplicateExample {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 2, 4, 3, 5};
        
        for (int i = 0; i < array.length - 1; i++) {
            if (array[i] != Integer.MIN_VALUE) {
                for (int j = i + 1; j < array.length; j++) {
                    if (array[i] == array[j]) {
                        array[j] = Integer.MIN_VALUE;
                    }
                }
            }
        }
        
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] != Integer.MIN_VALUE) {
                array[count++] = array[i];
            }
        }
        
        int[] deduplicatedArray = Arrays.copyOf(array, count);
        
        System.out.println(Arrays.toString(deduplicatedArray));
    }
}
Copy after login
  1. Using HashMap
    HashMap is a hash table structure in Java. Array elements are inserted as keys by using the put method of HashMap. If the key If it already exists, duplicate elements will be automatically removed.
    Code examples:
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ArrayDeduplicateExample {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 2, 4, 3, 5};
        
        Map<Integer, Object> map = new HashMap<>();
        for (int i : array) {
            map.put(i, null);
        }
        
        int[] deduplicatedArray = new int[map.size()];
        int index = 0;
        for (int i : map.keySet()) {
            deduplicatedArray[index++] = i;
        }
        
        System.out.println(Arrays.toString(deduplicatedArray));
    }
}
Copy after login

The above are detailed introductions and code examples of five classic Java array deduplication algorithms. In practical applications, choosing an appropriate deduplication algorithm according to specific circumstances can improve the performance and readability of the program.

The above is the detailed content of Detailed explanation of five classic Java array deduplication algorithms. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template