In: Computer Science
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();
}
}
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.