Step 1: Design Page – IntersectionList.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="IntersectionList.aspx.cs" Inherits="IntersectionList" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Find Intersection of Two Lists - Real Time Example</title>
<style>
body {
font-family: Arial;
background-color: #f2f5fc;
margin: 50px;
}
.container {
width: 550px;
margin: auto;
background: #fff;
border-radius: 10px;
box-shadow: 0 0 10px #ccc;
padding: 25px;
}
h2 {
text-align: center;
color: #1A2A80;
}
.form-control {
width: 100%;
padding: 8px;
margin-top: 10px;
}
.btn {
background-color: #7A85C1;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
width: 100%;
margin-top: 15px;
}
.btn:hover {
background-color: #5b68a1;
}
.result {
margin-top: 20px;
font-weight: bold;
color: #333;
text-align: center;
white-space: pre-line;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h2>Find Intersection of Two Lists</h2>
<asp:Label ID="Label1" runat="server" Text="Enter List 1 (comma-separated):"></asp:Label><br />
<asp:TextBox ID="txtList1" runat="server" CssClass="form-control" placeholder="Example: 10,20,30,40,50"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="Enter List 2 (comma-separated):"></asp:Label><br />
<asp:TextBox ID="txtList2" runat="server" CssClass="form-control" placeholder="Example: 30,40,50,60,70"></asp:TextBox><br />
<asp:Button ID="btnFindIntersection" runat="server" Text="Find Intersection" CssClass="btn" OnClick="btnFindIntersection_Click" />
<asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
</div>
</form>
</body>
</html>
Step 2: Backend Logic – IntersectionList.aspx.cs
using System;
using System.Collections.Generic;
public partial class IntersectionList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnFindIntersection_Click(object sender, EventArgs e)
{
lblResult.Text = "";
string list1Text = txtList1.Text.Trim();
string list2Text = txtList2.Text.Trim();
if (string.IsNullOrEmpty(list1Text) || string.IsNullOrEmpty(list2Text))
{
lblResult.Text = "Please enter both lists.";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
try
{
// Convert input strings to integer lists
string[] arr1 = list1Text.Split(',');
string[] arr2 = list2Text.Split(',');
List<int> list1 = new List<int>();
List<int> list2 = new List<int>();
List<int> intersection = new List<int>();
foreach (string s in arr1)
list1.Add(Convert.ToInt32(s.Trim()));
foreach (string s in arr2)
list2.Add(Convert.ToInt32(s.Trim()));
// Find intersection manually (without LINQ)
foreach (int num in list1)
{
if (list2.Contains(num) && !intersection.Contains(num))
{
intersection.Add(num);
}
}
if (intersection.Count > 0)
{
lblResult.Text = "Common Elements (Intersection):\n" + string.Join(", ", intersection);
lblResult.ForeColor = System.Drawing.Color.Green;
}
else
{
lblResult.Text = "No common elements found between the two lists.";
lblResult.ForeColor = System.Drawing.Color.Red;
}
}
catch
{
lblResult.Text = "Invalid input! Please enter only numbers separated by commas.";
lblResult.ForeColor = System.Drawing.Color.Red;
}
}
}
Real-Time Example Output
Input 1
List 1 → 10,20,30,40,50
List 2 → 30,40,50,60,70
Output
Common Elements (Intersection):
30, 40, 50
Input 2
List 1 → 5,10,15
List 2 → 20,25,30
Output
No common elements found between the two lists.
Explanation
| Step | Description |
|---|
| 1 | Takes two lists as comma-separated input from user. |
| 2 | Converts them into List<int> using Split() and Convert.ToInt32(). |
| 3 | Loops through the first list and checks if each element exists in the second list. |
| 4 | If it does and is not already in the result, adds it to intersection. |
| 5 | Displays all common elements. |
Algorithm
1. Read List1 and List2 from user.
2. Split both lists using ',' and convert to integer lists.
3. Create an empty list 'intersection'.
4. For each element in List1:
If element exists in List2 and not already in intersection:
Add element to intersection.
5. Display intersection elements.
Sample Test Cases
| List 1 | List 2 | Output |
|---|
| 10,20,30,40 | 30,40,50,60 | 30, 40 |
| 1,2,3,4 | 5,6,7,8 | No common elements |
| 5,10,15 | 10,15,20,25 | 10, 15 |
Concepts Used
List<int> → To store numeric elements.
Contains() → To check if an element exists in another list.
foreach → To iterate through list elements.
string.Join() → To display the intersection neatly.