You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In below code we are using two for loop to assign the departemnts to students. Can we optimize the code
class Department
{
public string DepartmentName { get; set; }
}
class Student
{
public string StudentName { get; set; }
public string DepartmentName { get; set; }
public Department Dept { get; set; }
}
class Program
{
static void Match(List lstDept, List lstStd)
{
foreach (Student std in lstStd)
{
var dept = lstDept.FirstOrDefault(d => d.DepartmentName == std.DepartmentName);
if (dept != null)
{
std.Dept = dept;
}
}
}
}
1. Try below.
class Department
{
public string DepartmentName { get; set; }
}
class Student
{
public string StudentName { get; set; }
public string DepartmentName { get; set; }
public Department Dept { get; set; }
}
class Program
{
static void Match(List lstDept, List lstStd)
{
var deptDictionary = lstDept.ToDictionary(d => d.DepartmentName, d => d);
foreach (Student std in lstStd)
{
if (deptDictionary.TryGetValue(std.DepartmentName, out var dept))
{
std.Dept = dept;
}
else
{
std.Dept = null; // Set to null if department not found
}
}
}
}
2. Try Below
class Department
{
public string DepartmentName { get; set; }
}
class Student
{
public string StudentName { get; set; }
public string DepartmentName { get; set; }
public Department Dept { get; set; }
}
class Program
{
static void Match(List lstDept, List lstStd)
{
lstDept.Sort((d1, d2) => d1.DepartmentName.CompareTo(d2.DepartmentName));
foreach (Student std in lstStd)
{
var index = BinarySearchDepartment(lstDept, std.DepartmentName);
if (index >= 0)
{
std.Dept = lstDept[index];
}
}
}
static int BinarySearchDepartment(List lstDept, string departmentName)
{
int left = 0;
int right = lstDept.Count - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
int comparison = lstDept[mid].DepartmentName.CompareTo(departmentName);
if (comparison == 0)
{
return mid; // Match found
}
else if (comparison < 0)
{
left = mid + 1; // Search in the right half
}
else
{
right = mid - 1; // Search in the left half
}
}
return -1; // No match found
}
}