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 linear-search Java method to test if a String array is already sorted alphabetically. The...
Write a linear-search Java method to test if a String array is already sorted alphabetically. The prototype should be static boolean ifsorted(String [] s) { }
given an array, write code to scan the array for a particular purpose . use java
given an array, write code to scan the array for a particular purpose . use java
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
Array of Hope! Write code for a Java class ArrayPair with the following fields and methods:...
Array of Hope! Write code for a Java class ArrayPair with the following fields and methods: ArrayPair - arr1 [] : int - arr2 [] : int Fields - length1 : int - length2 : int + ArrayPair (int arraySize) Methods + add (int arrNumber, int value) : void + remove (int arrNumber) : void + equal () : boolean + greater () : boolean + print () : void Thus, there are two arrays of integers in the class...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT