How to remove duplicate elements from an array using java

import java.util.Arrays;

public class RemoveDuplicatesFromArray {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 2, 3, 4, 4, 5};
        int[] uniqueNumbers = removeDuplicates(numbers);
        System.out.println("Array after removing duplicates: " + Arrays.toString(uniqueNumbers));
    }

    private static int[] removeDuplicates(int[] arr) {
        return Arrays.stream(arr).distinct().toArray();
    }
}

 

Post your Answer