Please use Java and take the screenshot of Java compiler with explanation Given
ID: 3748374 • Letter: P
Question
Please use Java and take the screenshot of Java compiler with explanation
Given a sequential list with n numbers, represented in a one dimensional array. A)Write an algorithm to check if the list has any duplication. Explain the time complexity of your algorithm Can you think of a better algorithm to do the job? If so, briefly explain. B) Write an algorithm to find all the duplicate numbers in the list, if any. Explain the time complexity of your algorithm Can you think of a better algorithm to do the job? If so, briefly explain. .Explanation / Answer
A. Algorithm to check for dublicate number
start
declare an array arr with the sequential list values and boolean variable to check for duplication
Iterate the list by choosing the first element in the list i.e. arr[0]
compare it with the other element in the array starting from the next element i.e. arr[1]
compare both the number. If equals
set duplicate=true and break the loop
check if the duplicate==true
display "Duplicate numbers are present in the list"
check if the duplicate==false
display "No Dublicate numbers are present in the list"
Stop
B. Algorithm to count duplicate number
start
declare an array arr with the sequential list values and duplicate variable to count for duplication
declare count for counting duplicate value
Iterate the list by choosing the first element in the list i.e. arr[0]
compare it with the other element in the array starting from the next element i.e. arr[1]
compare both the number. If equals
set duplicate=true and increment count by one
check if the duplicate==true
display "Duplicate numbers are present in the list and show the count"
check if the duplicate==false
display "No Dublicate numbers are present in the list"
stop
SeqList.java
//class declaration
public class SeqList {
//main function declaration
public static void main(String[] args) {
//declaring the array variable to store data
int arr[]={1,3,4,7,2,4,6,7,9,3,78,45,3,32,46,32};
int number;//variable used to check the dublicate element
int count=0;//counting dublicate value in the list
boolean dublicate=false;//assume there are no dublicates in the list
//for checking duplicate element in list
//intitalizing the loop to iterate through all element in the loop
dublicate=false;
for(int i=0;i<arr.length;i++){//iterating till the size of the array
//choosing the first element and comparing it with other element in the list
number=arr[i];
//iterating from the next element in the list and check for dublication
for(int j=i+1;j<arr.length;j++){
if(number==arr[j]){
dublicate=true;
break;
}
}
if(dublicate==true){
System.out.println("Dublicate numbers are present in the list");
break;
}
}
if(dublicate==false){
System.out.println("No Dublicate numbers are present in the list");
}
//counting number of dublicates in the list
//intitalizing the loop to iterate through all element in the loop
for(int i=0;i<arr.length;i++){//iterating till the size of the array
//choosing the first element and comparing it with other element in the list
number=arr[i];
count=1;
//iterating from the next element in the list and check for dublication
for(int j=i+1;j<arr.length;j++){
if(number==arr[j]){
dublicate=true;
count++;
}
}
if(dublicate==true){
System.out.println("Dublicate numbers "+number+" present in the list "+count+" times");
dublicate=false;
}
}
if(dublicate==false){
System.out.println("No Dublicate numbers are present in the list");
}
}
}
Output:
Dublicate numbers are present in the list
Dublicate numbers 1 present in the list 1 times
Dublicate numbers 3 present in the list 3 times
Dublicate numbers 4 present in the list 2 times
Dublicate numbers 7 present in the list 2 times
Dublicate numbers 3 present in the list 2 times
Dublicate numbers 32 present in the list 2 times
No Dublicate numbers are present in the list
Time Complexity:
Time complexity of both the algorithm is N*N, as there are nested loop of order 2.