#include <iostream>
#include <unordered_map>
#include <vector>
struct Node {
int val;
Node* next;
Node(int v) : val(v), next(nullptr) {}
};
bool lookupArray(const std::vector<int>& arr, int target) {
for (int i = 0; i < (int)arr.size(); i++) {
if (arr[i] == target) return true; // check each element
}
return false;
}
bool lookupLinkedList(Node* head, int target) {
Node* curr = head;
while (curr != nullptr) {
if (curr->val == target) return true; // check current node
curr = curr->next; // move to next node
}
return false;
}
bool lookupHashMap(const std::unordered_map<int, int>& map, int target) {
return map.find(target) != map.end(); // direct lookup
}
int main() {
std::vector<int> arr = {1, 2, 3, 4};
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
std::unordered_map<int, int> map = {{1, 10}, {2, 20}, {3, 30}, {4, 40}};
int target = 3;
std::cout << "Array lookup: " << (lookupArray(arr, target) ? "Found" : "Not Found") << "\n";
std::cout << "Linked List lookup: " << (lookupLinkedList(head, target) ? "Found" : "Not Found") << "\n";
std::cout << "Hash Map lookup: " << (lookupHashMap(map, target) ? "Found" : "Not Found") << "\n";
// Clean up linked list
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
return 0;
}
if (arr[i] == target) return true; // check each element
check each array element until target found
if (curr->val == target) return true; // check current node
check current linked list node for target
curr = curr->next; // move to next node
advance to next node in linked list
return map.find(target) != map.end(); // direct lookup
directly check if target key exists in hash map
Array lookup: Found
Linked List lookup: Found
Hash Map lookup: Found