Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 limport java util.Random 4 public class ArrayNum1 static Random r new Random (

ID: 3575734 • Letter: 1

Question

1 limport java util.Random 4 public class ArrayNum1 static Random r new Random (10) static int new int 20 array public static void main (String[] args) creates an array of 20 elements assigns random numbers to the 20 elements from 1-20 for (int i 0; ik array.length; itt) 11 array[i] r next Int(20)+1; 12 13 prints the array 14 printArray(); 15 //calls duplicate method 16 duplicate method(); 17 system out.println("MnNo DUPS"); 18 printArray(); 19 20 21 22e public static void duplicate method( f int number 23 int count 0; 25 boolean flag true while(flag) 26 flag false 27 remove duplicates 28 for (int i- 0; i k array length i++) 29 for (int j i+1 j array length; j++) 30 loops through each number in the array to see if there are duplicates 31 if (array[i] array[j]){ 32 //if there are any duplicates the duplicate's value will be changed to 33 array [j] 0; 34 flag true 35 36 37 38 system out.println("VnDups removed:"); printArray(); replace duplicates with zero for (int i 0; i array. length; i++) 41 if (array[i 0) array[i] next Int (20)+1; 42 43 system out.println("VnRepopulated: "),printArray0, 45 46 47e public static void printArray() for (int i 0; i array. length -1; it+) 48 System. out.print (arrayli) 50 51

Explanation / Answer

answer:

at line 26 in the program the flag was initialized to true because in order to go first time into while loop. Then inside the loop, at 27 the flag was turned to false, because in order to remove duplicate values every time from that array, loop should be iterated till last value. if we keep true always it becomes infinite loop. After all the iterations of inner “for loop” are done flag becomes false and hence at line 26, the loop will exit and it will returns the array without duplicates