Mid From PDF Coding Scenarios C# Coding Interview

Remove duplicate elements from an array (without?

LINQ Distinct())

Logic

  • Use a HashSet to track seen elements.
  • Add elements only if they are not already present.
int[] arr = { 1, 2, 3, 2, 4, 1 };
HashSet<int> set = new HashSet<int>();
List<int> result = new List<int>();
foreach (int num in arr)
{
if (!set.Contains(num))
{

set.Add(num);

result.Add(num);

}
}

Why this works:

HashSet ensures uniqueness with O(1) lookup.

More from C# Programming Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details