Question

In: Computer Science

Sorting and Searching Given an unsorted array numbers of integers with duplicate values. Sort the array...

Sorting and Searching

Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top of your code.

Hints: Sort the array before finding the number of unique elements. You can use any sorting algorithm that you have learned in the class. Do not use the built-in sorting algorithm of java.

Clarification:

Confused why the returned value of removeDuplicates() method is an integer but your answer is an array?

Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller (main() method) as well.

Example 1:

Input: {1, 2, 1}

Output: {1, 2}

Your removeDuplicates() method should return length = 2, with the two unique elements of numbers being 1 and 2. It doesn't matter what you leave beyond the returned length.

Example 2:

Input: {2, 1, 0, 1, 2, 0, 3, 3, 2, 1}

Output: {0, 1, 2, 3}

Your removeDuplicates() method should return length = 4, with the four unique elements of numbers are 0, 1, 2, and 3. It doesn't matter what values are set beyond the returned length.

USE THIS CODE:

import java.util.Scanner;

class Main {
  
// add your sorting algorithm code here
  
public static int removeDuplicates(int[] numbers){
// your code goes here for removing duplicates
  
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the size of the array > ");
int n = scan.nextInt();
int[] numbers = new int[n];
System.out.print("Enter the array elements with duplicates in it > ");
for(int i = 0; i < n; i++){
numbers[i] = scan.nextInt();
}
int len = removeDuplicates(numbers);
for(int i = 0; i < len; i++)
System.out.print(numbers[i] + " ");
System.out.println();
scan.close();
}
}

Solutions

Expert Solution

SOLUTION TO YOUR QUESTION

The method removeDuplicates(int[] numbers) uses O(n2) Time Complexity

Code to copy:

import java.util.*;

class Main {
  
// This Method uses O(n^2) Time Complexity
// add your sorting algorithm code here
public static int removeDuplicates(int[] numbers){
int i, key, j, l=0;
  
// USING INSERTION SORT
for(i=1; i<numbers.length; i++){
key = numbers[i];
j = i-1;
  
while(j>=0 && numbers[j]>key){
numbers[j+1] = numbers[j];
j -=1;
}
numbers[j+1] = key;
}
  
// REMOVING DUPLICATE VALUES
for(int k=0; k<numbers.length-1; k++){
if(numbers[k]!=numbers[k+1]){
numbers[l++] = numbers[k];
}
}
  
numbers[l++] = numbers[numbers.length - 1];
  
return l;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
  
System.out.print("Enter the size of the array > ");
int n = scan.nextInt();
int[] numbers = new int[n];
  
System.out.print("Enter the array elements with duplicates in it > ");
for(int i = 0; i < n; i++){
numbers[i] = scan.nextInt();
}
  
int len = removeDuplicates(numbers);
  
for(int i = 0; i < len; i++)
System.out.print(numbers[i] + " ");
System.out.println();
scan.close();
}
}

SNAPSHOTS OF THE CODE:

SAMPLE OUTPUT:


Related Solutions

IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. (Write a C# program)
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. C++ program thanks
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. I need this in java language.
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. I need this in java language. Radix Sort assignment (CISP430 ignore this part) This is only for those who are using Java How many classes do I need? A: Node, Queue, Radix, Driver...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions: • myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise. • search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array). • myRemove ( ): Step...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax. (no [ ]'s or (ptr+1) stuff. input will be the number of input values to enter followed by the sum to compare with. print out the continuous sub-array of values that are equal to sum or the message 'no sum ofund.' there may be more than one sub-array to be found in...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
IN JAVA Methods**: Sort three values Write a method Ascend3 with an array of integers of...
IN JAVA Methods**: Sort three values Write a method Ascend3 with an array of integers of size three as the parameter, that sorts the values of the array into ascending order. Ex: If the array contains [5, 2, 7], after the call Ascend3(int[] vals), the array will now hold [2, 5, 7]. Hints: Return type should be void. One approach puts the three values into an array, then sorts the array. We won't be describing that approach here. Instead, we'll...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT