public class ProgrammingProject4 { public static void main(String[] args) { //cr
ID: 3574655 • Letter: P
Question
public class ProgrammingProject4 {
public static void main(String[] args) {
//create array
//Initialize array
//display array
//call duplicate method
if(duplicate(num))
System.out.println("This array has duplicates. ");
else
System.out.println("This array does not have any duplicates. ");
}
/*
* method name: duplicate
* input: int[]
* output: boolean
* description: indicate whether the input integer array contains any duplicates
*/
public static boolean duplicate(int[] num){
}
}
Sample run 1:
25 35 79 37 80 57 75 95 87 30 33 7 91 59 77 12 63 59 80 57
This array has duplicates.
Sample run 2:
45 22 11 32 20 13 53 41 0 48 29 59 97 9 64 72 61 73 51 4
This array does not have any duplicates.
Explanation / Answer
ProgrammingProject4.java
import java.util.Arrays;
public class ProgrammingProject4 {
public static void main(String[] args) {
//create array
//Initialize array
int num[] = {25,35,79,37,80,57,75,95, 87, 30, 33, 7, 91, 59 , 77, 12, 63, 59, 80, 57 };
//display array
System.out.println(Arrays.toString(num));
//call duplicate method
if(duplicate(num))
System.out.println("This array has duplicates. ");
else
System.out.println("This array does not have any duplicates. ");
}
/*
* method name: duplicate
* input: int[]
* output: boolean
* description: indicate whether the input integer array contains any duplicates
*/
public static boolean duplicate(int[] num){
int count = 0;
for(int i=0; i<num.length; i++){
count = 0;
for(int j=i+1; j<num.length; j++){
if(num[i] == num[j]){
count = 1;
break;
}
}
if(count == 1){
return true;
}
}
return false;
}
}
Output:
[25, 35, 79, 37, 80, 57, 75, 95, 87, 30, 33, 7, 91, 59, 77, 12, 63, 59, 80, 57]
This array has duplicates.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.