int[] arr = {5, 10, 15, 20, 25};
int[] searchValues = {10, 25, 30};
10 found at index 1 25 found at index 4 30 not found
class SearchMultipleElements {
public static void main(String[] args) {
int[] arr = {5, 10, 15, 20, 25};
int[] searchValues = {10, 25, 30};
for (int i = 0; i < searchValues.length; i++) {
boolean found = false;
for (int j = 0; j < arr.length; j++) {
if (arr[j] == searchValues[i]) {
System.out.println(searchValues[i] + " found at index " + j);
found = true;
break;
}
}
if (!found) {
System.out.println(searchValues[i] + " not found");
}
}
}
}
The program uses nested loops to search multiple values one by one inside the same array.
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.