Home Java javaTutorial Detailed explanation of five classic Java array deduplication algorithms

Detailed explanation of five classic Java array deduplication algorithms

Dec 23, 2023 am 10:01 AM
java array Deduplication algorithm Classic algorithm

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Revealing Five Efficient Java Array Deduplication Methods Revealing Five Efficient Java Array Deduplication Methods Dec 23, 2023 pm 02:46 PM

Five efficient Java array deduplication methods revealed In the Java development process, we often encounter situations where we need to deduplicate arrays. Deduplication is to remove duplicate elements in an array and keep only one. This article will introduce five efficient Java array deduplication methods and provide specific code examples. Method 1: Use HashSet to deduplicate HashSet is an unordered, non-duplicate collection that automatically deduplicates when adding elements. Therefore, we can use the characteristics of HashSet to deduplicate arrays. public

Common ways to add elements to Java arrays Common ways to add elements to Java arrays Feb 21, 2024 am 11:21 AM

Common ways to add elements to Java arrays, specific code examples are required In Java, an array is a common data structure that can store multiple elements of the same type. In actual development, we often need to add new elements to the array. This article will introduce common methods of adding elements to arrays in Java and provide specific code examples. A simple way to create a new array using a loop is to create a new array, copy the elements of the old array into the new array, and add the new elements. The code example is as follows: //original array i

What are the common methods of java arrays? What are the common methods of java arrays? Jan 02, 2024 pm 04:49 PM

Commonly used methods include length attribute, copy array, array traversal, array sorting, array conversion to string, etc. Detailed introduction: 1. Length attribute: used to get the length of an array. It is an attribute rather than a method. Example: int[] arr = {1, 2, 3}; int length = arr.length;; 2. Copy the array: Use the System.arraycopy() method or the copyOf() method of the Arrays class to copy the contents of the array to a new Arrays etc.

Detailed explanation of five classic Java array deduplication algorithms Detailed explanation of five classic Java array deduplication algorithms Dec 23, 2023 am 10:01 AM

Detailed explanation of five classic Java array deduplication algorithms In Java programming, you often encounter situations where you need to perform deduplication operations on arrays, that is, remove duplicate elements in the array and retain unique elements. The following will introduce five classic Java array deduplication algorithms and provide corresponding code examples. 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: importjava.util.Arr

How to use arrays and collections for data storage and manipulation in Java How to use arrays and collections for data storage and manipulation in Java Oct 18, 2023 am 08:15 AM

How to use arrays and collections for data storage and operation in Java In Java programming, arrays and collections are commonly used methods of data storage and operation. An array is a container used to store data of the same type, while a collection is an object composed of multiple elements. The basic method of using arrays for data storage and manipulation is as follows: Declaring an array variable To use an array, you first need to declare an array variable. An array variable can be declared using the following syntax: dataType[]arrayName; where dataT

Solution to ArrayIndexOutOfBoundsException exception in Java Solution to ArrayIndexOutOfBoundsException exception in Java Jun 25, 2023 am 11:02 AM

Java is a widely used programming language that provides programmers with many practical and powerful tools and features. When writing Java programs, you may encounter various exceptions. Among them, ArrayIndexOutOfBoundsException is a common exception. This exception is triggered when we try to access an element that does not exist in the array. In this article, we will discuss ArrayIndexOutOfBoundsExc in Java in detail

Tips and things to note when adding elements to an array in Java Tips and things to note when adding elements to an array in Java Jan 03, 2024 pm 02:01 PM

Tips and precautions for adding elements to arrays in Java In Java, arrays are a very common and important data structure. It can store a set of elements of the same type, and these elements can be accessed and modified through indexes. In practical applications, we often need to dynamically add elements to an array. This article will introduce some tips and precautions for adding elements to arrays in Java, and provide corresponding code examples. Use dynamic array (ArrayList) to add elements Dynamic array ArrayList is

An in-depth analysis of five practical methods for deduplicating Java arrays An in-depth analysis of five practical methods for deduplicating Java arrays Dec 23, 2023 am 09:21 AM

An in-depth analysis of five practical methods for deduplicating Java arrays. In Java, processing arrays is a very common operation. Array deduplication is a problem often encountered in actual development. This article will provide an in-depth analysis of five practical methods for Java array deduplication and provide specific code examples. 1. Use HashSet to remove duplicates. HashSet is a collection in Java that has the function of automatic deduplication. We can use the characteristics of HashSet to add elements in the array to HashSet to achieve the effect of deduplication.

See all articles