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

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
Given a sorted array with lot of duplicates, write a problem to search specific element m....
Given a sorted array with lot of duplicates, write a problem to search specific element m. If it’s included in the A, return its minimal index, otherwise return -1. For example A = {0, 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 4}, if we search 4, you program should return 8. You are only allowed to use binary search. Linear search is not allowed here.
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Write a Java class called Employee (Parts of the code is given below), which has three...
Write a Java class called Employee (Parts of the code is given below), which has three private fields firstName (String), lastName (String) and yearsEmployed (double). Implement accessor/mutator methods for the private fields and toString() method, that returns a String representation, which contains employee name and years she was employed. public class Employee { private String firstName; ... } Write a client program called EmployeeClient that creates an instance of Employee class, sets values for the fields (name: John Doe, yearsEmployed:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT