Write a Java program to remove the duplicate elements of a given array and return the new length of the array.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
Question
Write a Java program to remove the duplicate elements of a given array and return the new length of the array.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
Solution 1
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[] array = {20, 20, 30, 40, 50, 50, 50};
int newLength = removeDuplicates(array);
System.out.println("The new length of the array is: " + newLength);
}
public static int removeDuplicates(int[] array) {
if (array.length == 0) {
return 0;
}
int i = 0;
for (int j = 1; j < array.length; j++) {
if (array[j] != array[i]) {
i++;
array[i] = array[j];
}
}
return i + 1;
}
}
This program works by iterating over the array and comparing each element with the previous one. If the current element is different from the previous one, it is copied to the next position of the new array. The counter i keeps track of the position in the new array, and is incremented each time a new unique element is found. The new length of the array is i + 1, because array indices start at 0.
Please note that this program assumes that the input array is sorted. If the input array is not sorted, you should sort it first before removing duplicates.
Solution 2
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[] array = {20, 20, 30, 40, 50, 50, 50};
int length = removeDuplicates(array);
System.out.println("The new length of the array is " + length);
}
public static int removeDuplicates(int[] array) {
if (array.length == 0) {
return 0;
}
int i = 0;
for (int j = 1; j < array.length; j++) {
if (array[j] != array[i]) {
i++;
array[i] = array[j];
}
}
return i + 1;
}
}
This program works by iterating over the array and comparing each element with the previous one. If the current element is different from the previous one, it is copied to the next position of the new array. The length of the new array is then returned.
Please note that this program assumes that the input array is sorted. If the input array is not sorted, you should sort it first before removing duplicates.
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}
length of the longest subarrayGiven an array of elements, return the length of the longest subarray where all its elements are distinct.For example,given the array `[5, 1, 3, 5, 2, 3, 4, 1]`, return `5` as the longest subarray of distinct elements is `[5, 2, 3, 4, 1]`.Sample Test CasesTest Case 1:Expected Output:Enter·elements·of·the·list·separated·by·,(comma):·5,1,3,5,2,3,4,1Unique·no.of·elements·are:·5Test Case 2:Expected Output:Enter·elements·of·the·list·separated·by·,(comma):·5,5,5Unique·no.of·elements·are:·1
442. Find All Duplicates in an ArrayMediumTopicsCompaniesGiven an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice.You must write an algorithm that runs in O(n) time and uses only constant extra space. Example 1:Input: nums = [4,3,2,7,8,2,3,1]Output: [2,3]Example 2:Input: nums = [1,1,2]Output: [1]Example 3:Input: nums = [1]Output: [] Constraints:n == nums.length1 <= n <= 1051 <= nums[i] <= nEach element in nums appears once or twice.
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
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:
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.