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
Solution 1
Sure, here is a simple Java program that removes duplicate elements from an array using a HashSet, which does not allow duplicate values. The program then returns the new length of the array.
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
int[] array = {1, 2, 2, 3, 4, 4, 5, 5, 6};
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 define 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 values, all duplicates are automatically removed. Finally, we return the size of the set, which is the new length of the array without duplicates.
Solution 2
Sure, here is a simple Java program that removes duplicate elements from an array using a HashSet, which does not allow duplicate values. The program then returns the new length of the array.
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
int[] array = {1, 2, 2, 3, 4, 4, 5, 5, 6};
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, the removeDuplicates method takes an array as input and adds each element to a HashSet. Since a HashSet does not allow duplicate values, any duplicate elements in the array are automatically removed. The method then returns the size of the HashSet, which is the new length of the array.
Solution 3
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, 5, 6};
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<Integer>();
for (int i = 0; i < array.length; i++) {
set.add(array[i]);
}
return set.size();
}
}
In this program, the removeDuplicates method takes an array as input and adds each element to a HashSet. Since a HashSet does not allow duplicate elements, any duplicates in the array are automatically removed. The method then returns the size of the HashSet, which is the new length of the array without duplicates.
Solution 4
Here is a simple Java program that removes duplicate elements from an array and returns the new length of the array:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] nums = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5};
int newLength = removeDuplicates(nums);
System.out.println("The new length of the array is: " + newLength);
}
public static int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}
In this program, we first sort the array. Then we use two pointers, i and j, to traverse the array. i is the slow-runner while j is the fast-runner. As long as nums[i] == nums[j], we increment j to skip the duplicate. When we encounter nums[j] != nums[i], the duplicate run has ended so we must copy its value to nums[i + 1]. i is then incremented and we repeat the same process again until j reaches the end of array.
Similar Questions
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]
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}
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]); } }}
Problem StatementRajini is a student who loves coding. He is working on an array operations problem. In the given problem, Rajini needs to remove the first element from the array and print the modified array.Help Rajini write a program that takes an array of integers as input, deletes the first element from the array, and prints the modified array.Input format :The first line of input is an integer n, representing the number of elements in the array.The second line of input consists of n space-separated integers, representing the elements of the array arr[i].Output format :The output displays n space-separated integers of the modified array after deleting the first element.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases will fall under the following constraints:2 ≤ n ≤ 101 ≤ arr[i] ≤ 100Sample test cases :Input 1 :56 7 4 3 1Output 1 :7 4 3 1 Input 2 :210 47Output 2 :47 Input 3 :1037 84 27 23 48 19 1 38 27 39 100Output 3 :84 27 23 48 19 1 38 27 39
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.