using System;
using System.Collections.Generic;
class Program
{
// Array lookup by value
static int ArrayLookup(int[] arr, int value)
{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == value)
return i; // found at index i
}
return -1; // not found
}
// Linked list node
class Node
{
public int val;
public Node next;
public Node(int v) { val = v; next = null; }
}
// Linked list lookup by traversal
static Node LinkedListLookup(Node head, int value)
{
Node curr = head;
while (curr != null)
{
if (curr.val == value)
return curr; // found node
curr = curr.next; // move to next node
}
return null; // not found
}
// Hash map lookup using Dictionary
static bool HashMapLookup(Dictionary<int, int> map, int key, out int value)
{
return map.TryGetValue(key, out value); // direct lookup
}
static void Main()
{
int[] arr = {1, 2, 3, 4};
Console.WriteLine("Array lookup for 3 at index: " + ArrayLookup(arr, 3));
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
Node foundNode = LinkedListLookup(head, 3);
Console.WriteLine("Linked list lookup for 3: " + (foundNode != null ? "found" : "not found"));
var map = new Dictionary<int, int> {{1, 1}, {2, 2}, {3, 3}, {4, 4}};
if (HashMapLookup(map, 3, out int val))
Console.WriteLine("Hash map lookup for key 3: found value " + val);
else
Console.WriteLine("Hash map lookup for key 3: not found");
}
}if (arr[i] == value) return i; // found at index i
check each array element until value found
while (curr != null) { if (curr.val == value) return curr; curr = curr.next; }
traverse linked list nodes until value found or end
return map.TryGetValue(key, out value);
directly check hash map bucket for key
Array lookup for 3 at index: 2
Linked list lookup for 3: found
Hash map lookup for key 3: found value 3