What does the following piece of code do?for (int i = 0; i < arr.length-1; i++){ for (int j = i+1; j < arr.length; j++) { if( (arr[i].equals(arr[j])) && (i != j) ) { System.out.println(arr[i]); } }}OptionsPrint the duplicate elements in the arrayPrint the unique elements in the arrayPrints the element with minimum frequnecyPrint the element with maximum frequency
Question
What does the following piece of code do?for (int i = 0; i < arr.length-1; i++){ for (int j = i+1; j < arr.length; j++) { if( (arr[i].equals(arr[j])) && (i != j) ) { System.out.println(arr[i]); } }}OptionsPrint the duplicate elements in the arrayPrint the unique elements in the arrayPrints the element with minimum frequnecyPrint the element with maximum frequency
Solution
The given piece of code prints the duplicate elements in the array.
Here's a step-by-step explanation:
-
The outer loop starts from the first element of the array and iterates through each element until the second last element (because of
arr.length-1). -
The inner loop starts from the element next to the current element of the outer loop (because of
i+1) and iterates through each element until the last element. -
In each iteration of the inner loop
Similar Questions
What does the following piece of code do?for (int i = 0; i < arr.length-1; i++){ for (int j = i+1; j < arr.length; j++) { if( (arr[i].equals(arr[j])) && (i != j) ) { System.out.println(arr[i]); } }}OptionsPrint the duplicate elements in the arrayPrint the unique elements in the arrayPrints the element with minimum frequnecyPrint the element with maximum frequency
What's the output of the following code?public static void main(String[] args) { int[] vals = {4, 3, 2, 1}; for (int v : vals) { System.out.println(indexOf(vals, v)); }}public static int indexOf(int[] all, int element) { for (int i = 0; i < all.length; i++) { if (all[i] == element) return i; } return -1;}
What is output of the following code:public class Test{ public static void main(String[] args){ int[] x = {120, 200, 016 }; for(int i = 0; i < x.length; i++) System.out.print(x[i] + " "); }
What is the output of the following Java code?public class ArrayComparison { public static void main(String[] args) { int[] arr1 = {1, 2, 3}; int[] arr2 = {1, 2, 3}; int[] arr3 = arr1; System.out.println(arr1 == arr2); System.out.println(arr2 == arr3); }}
What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; int v = values[0][0]; for (int row = 0; row < values.length; row++) for (int column = 0; column < values[row].length; column++) if (v < values[row][column]) v = values[row][column]; System.out.print(v); } } Group of answer choices135633
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.