Knowee
Questions
Features
Study Tools

Write a Java program to remove the duplicate elements of a given array and return the new length of the array.

Question

Write a Java program to remove the duplicate elements of a given array and return the new length of the array.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a simple Java program that removes duplicate elements from an array and returns the new length of the array. This program uses a HashSet, which does not allow duplicate elements.

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        int[] array = {1, 2, 2, 3, 4, 4, 5, 6, 6, 7};
        int newLength = removeDuplicates(array);
        System.out.println("The new length of the array is: " + newLength);
    }

    public static int removeDuplicates(int[] array) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            set.add(array[i]);
        }
        return set.size();
    }
}

In this program, we first initialize an array with some duplicate elements. We then call the removeDuplicates method, which takes this array as an argument. Inside this method, we create a HashSet and add all elements of the array to this set. Since a set does not allow duplicate elements, all duplicates are automatically removed. Finally, we return the size of the set, which is the new length of the array without duplicates.

This problem has been solved

Similar Questions

Copy the skeleton of code below into your answer.  Then in the space indicated, add your own code to write a method that takes two arrays  and returns a new array the LENGTH OF THE SHORTER of the two where at each position if the two elements don't match there is a 0 and if the two elements do match there is a copy of that value.  The two original arrays should NOT be changed by this method.For example, if the calling method makes the callint[] array1 = {2, 4, 5, 7, 3, 5, 7, 88};int[] array2 = {8, 4, 3, 0, 3};int[] result = matches(array1,array2);After the call finishes, array and result will have the values:result = {0, 4, 0, 0, 3}array1 = {2, 4, 5, 7, 3, 5, 7, 88}array2 = {8, 4, 3, 0, 3}As another example, if the calling method makes the callint[] array1 = {8, 3, 7};int[] array2 = {8, 9, 7, 9, 4};int[] result = matches(array1,array2);After the call finishes, array and result will have the values:result = {8, 0, 7}array1 = {8, 3, 7}array2 = {8, 9, 7, 9, 4} If the method is called with any 0-length array it should return a new 0-length array:int[] array1 = {2, 4};int[] array2 = {};int[] result = matches(array1, array2);After the above call finishes, array and result will have the values:result = {}array1 = {2, 4}array2 = {}Use the skeleton of the method below to write your code.public static int[] matches(int[] arr1, int[] arr2) {   // TODO: Replace with your code}

Write a JAVA program to create an Array List for objects of type String, and then several strings are added to it. The list is then displayed. Some of the elements are removed and the list is displayed again.Input form:Initial size of an array:Size of an array after additions:Contents of an array:Size of an array after deletions:Contents of an array:Output form:Initial size of an array: Size of an array after additions: Contents of an array: Size of an array after deletions: Contents of an array:

Given an array and a key, the task is to remove all occurrences of the specified key from the array in Java. Examples:Input: array = { 3, 9, 2, 3, 1, 7, 2, 3, 5 }, key = 3Output: [9, 2, 1, 7, 2, 5]

for (int i = 0; i < arr.length-1; i++){ for (int j = i+1; j < arr.length; j++) { if( (arr[i].equals(arr[j])) && (i != j) ) { System.out.println(arr[i]); } }}

Write a program to eliminate the common elements in the given 2 arrays and print only the non-repeating elements and the total number of such non repeating elements.Input Format:The first line contains space separated values, denoting the size of the two arrays in integer format respectively.The next two lines contains the space-separated integer arrays to be compared.Sample Input:5 41 2 8 6 52 6 8 10Sample Output:1 5 103

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.