Question

In: Computer Science

in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...

in java code

In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method:

public static int removeDuplicates(int[ ] A)

For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:

  • Return 5 as the number of distinct elements.

  • Have distinct elements 0, 1, 2, 3, 4 occupy the first 5 slots of the original array A. That is, after the method, the array A should look like A=0, 1, 2, 3, 4, *, *, *, *, *, where each * is a “don’t care”, that is, we don’t care what element occupies that slot.

Your method must have time complexity On and space complexity O1, where n is the length of the input array.

Hint: Use two pointers.

Solutions

Expert Solution

public class Main
{
public static int removeDuplicates(int[ ] A)
{
int len=A.length,k=0;
for(int i=0;i<len;)
{
int j=i+1;
while(j<len&&A[j]==A[i])
{
j++;
}
if(j<len)
{
k++;
A[k]=A[j];
}
i=j;
}
return (len>0?(k+1):0);
}
   public static void main(String[] args)
   {
   int[] A = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
       System.out.println(removeDuplicates(A));
for(int i=0;i<A.length;i++)
       System.out.print(A[i]+" ");
   }
}


Related Solutions

Given an array, write code to scan the array for a particular purpose. Given a class,...
Given an array, write code to scan the array for a particular purpose. Given a class, write a getter and/or a setter. Given a class with a list, write code that manages the list.
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items. Anytime the list becomes full, double the size of the array.
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Please write a Java method contains that checks whether the second given character array is contained...
Please write a Java method contains that checks whether the second given character array is contained in the first given character array. We require that both of the arrays are partially filled.
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the help of : (a) insertion sort; (b) selection sort; (c) bubble sort with swaps counting; (d) bubble sort without swaps counting subject design and analysis of algorithms answer should be like eg p u r p l e p r u p l e
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT