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

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 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.
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 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
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Using Java please You are given an array of integers arr. Your task is to count...
Using Java please You are given an array of integers arr. Your task is to count the number of contiguous subarrays, such that each element of the subarray appears at least twice. E.g For arr = [0, 0, 0], the output should be duplicatesOnSegment(arr) = 3.
Create a Python program that will take an unsorted list of 1000 integers and sort them...
Create a Python program that will take an unsorted list of 1000 integers and sort them using a bubble sort and an insertion sort. Your output should include displaying both sorted lists (from each algorithm) identifying each sorted list by the algorithm used.
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
problem 1 (Duplicate Elimination) code in JAVA please Use a one-dimensional array to solve the following...
problem 1 (Duplicate Elimination) code in JAVA please Use a one-dimensional array to solve the following problem: Write an application that inputs ten numbers, each between 10 and 100, both inclusive. Save each number that was read in an array that was initialized to a value of -1 for all elements. Assume a value of -1 indicates an array element is empty. You are then to process the array, and remove duplicate elements from the array containing the numbers you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT