w3resource

Java: Remove the duplicate elements of a given array and return the new length of the array

Java Array: Exercise-33 with Solution

Write a Java program to remove duplicate elements from a given array and return the updated array length.
Sample array: [20, 20, 30, 40, 50, 50, 50]
After removing the duplicate elements the program should return 4 as the new length of the array.

Pictorial Presentation:

Java Array Exercises: Remove the duplicate elements of a given array and return the new length of the array

Sample Solution:

Java Code:

// Define a class named Exercise33.
public class Exercise33 {
    // The main method for executing the program.
    public static void main(String[] args) {
        // Declare an array of integers.
        int nums[] = {20, 20, 30, 40, 50, 50, 50};  

        // Print the original array length.
        System.out.println("Original array length: " + nums.length);

        // Print the elements of the array.
        System.out.print("Array elements are: ");
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + " ");
        }

        // Calculate the new length of the array using the array_sort method and print it.
        System.out.println("\nThe new length of the array is: " + array_sort(nums));
    }

    // Define a method named array_sort that takes an array of integers as input.
    public static int array_sort(int[] nums) {
        // Initialize an index variable to 1.
        int index = 1;
        
        // Iterate through the array, starting from the second element.
        for (int i = 1; i < nums.length; i++) {
            // Check if the current element is different from the previous element.
            if (nums[i] != nums[index - 1])
                // If different, update the element at the current index.
                nums[index++] = nums[i];
        }
        // Return the new length of the array.
        return index;
    }
}

Sample Output:

                                                                              
Original array length: 7                                               
Array elements are: 20 20 30 40 50 50 50                               
The new length of the array is: 4

Flowchart:

Flowchart: Java exercises: Remove the duplicate elements of a given array and return the new length of the array

Java Code Editor:

Previous: Write a Java program to check if an array of integers contains two specified elements 65 and 77.
Next: Write a Java program to find the length of the longest consecutive elements sequence from a given unsorted array of integers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.