Question

In: Computer Science

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 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.

CODE:

I need time complexity too

// time complexity of your 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

import java.util.Scanner;

public class Main {
public static void sort(int[] a){//bubble sort algorithm
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-i-1;j++){
if(a[j]>a[j+1]){//swap if the current element is greater than next element
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
public static int removeDuplicates(int[] numbers){
sort(numbers);//sort the array
int c=1;
int i,j=0;
  
for( i=1;i<numbers.length;i++){
if(numbers[i]==numbers[i-1])
continue;
else//increment count value if duplicates occurs
c++;
}
for(i=0;i<numbers.length;i++){
if(i<numbers.length-1 &&numbers[i]==numbers[i+1])//continue loop if duplicates
continue;
numbers[j++]=numbers[i];//modify the same array if the current element is not duplicate
}

return c;//return length
}
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();
}
}

Screenshots:

The screenshots are attached below for reference.

Please follow them for output.

Please upvote my answer. Thank you.


Related Solutions

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...
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...
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...
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...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
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...
JAVA Write a method that removes the duplicate elements from an array list of integers using...
JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run: Enter ten integers: 10 20 30 20 20 30 50 60 100 9 The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT