Multiple Targets Search Algorithm in Java

The Multiple Targets Search Algorithm is a method in Java programming used to search for multiple values simultaneously within an array or list. This approach optimizes the search process and saves time by searching for multiple values at once.

How the Multiple Targets Search Algorithm Works

The Multiple Targets Search Algorithm works by iterating through each element of the array or list and comparing them with a list of target values to be searched. If an element in the array matches a target value, it is added to the result list.

Advantages and Disadvantages of the Multiple Targets Search Algorithm


  • Good Performance: This algorithm searches for multiple values in one go, saving time compared to performing multiple separate searches.
  • Versatile: Can be applied in various scenarios that require searching for multiple targets.


  • Memory Consumption: Due to the need to store the result list, this algorithm may consume more memory compared to simple searches.

Example and Explanation

Consider an example of using the Multiple Targets Search Algorithm to find multiple specific integers in an integer array in Java.

import java.util.ArrayList;
import java.util.List;

public class MultipleTargetsSearchExample {
    public static List<Integer> multipleTargetsSearch(int[] array, int[] targets) {
        List<Integer> results = new ArrayList<>();

        for (int target : targets) {
            for (int i = 0; i < array.length; i++) {
                if (array[i] == target) {
                    results.add(i); // Add position to results if found

        return results;

    public static void main(String[] args) {
        int[] numbers = { 4, 2, 7, 2, 9, 5, 7 };
        int[] targets = { 2, 7 };

        List<Integer> positions = multipleTargetsSearch(numbers, targets);

        if (!positions.isEmpty()) {
            System.out.println("Targets found at positions: " + positions);
        } else {
            System.out.println("Targets not found in the array");

In this example, we use the Multiple Targets Search Algorithm to find the numbers 2 and 7 within an integer array. The algorithm iterates through the array and compares each element with the list of target values. In this case, the number 2 is found at positions 1 and 3, and the number 7 is found at positions 2 and 6 in the array.

While this example demonstrates how the Multiple Targets Search Algorithm can search for multiple values at once, it can also be applied to various search scenarios in Java programming.