This problem may look simple, but it’s a great way to understand loops, collections, and LINQ in .NET.
In this blog, we’ll go through: Understanding the problem Finding Duplicate Numbers in an Array using C# (Step-by-Step with Example & LINQ)
Writing a custom logic to detect duplicates and Implementing the same with LINQ. Walking through an example step by step. Explaining why this is useful in real projects
Let’s dive in. Problem Statement
We are given an array of integers: int[] arr = {12, 13, 14, 14, 13, 12, 11, 15};
If you need to figure out the repeating numbers in this array. In the above case,
12 appears more than once
13 appears more than once
14 appears more than once
So, the output should be: 12, 13, 14
Solution 1: Finding Duplicates using Custom Logic
Let’s look at a step-by-step custom logic implementation:
When working with arrays in C#, one common challenge is identifying duplicate elements—numbers that appear more than once. Detecting duplicates is not only useful for interview coding questions but also for real-world scenarios like data validation, cleaning datasets, or preventing duplicate records from entering a system.
In this post, we’ll go through a practical example of finding duplicate numbers in an array using a manual logic approach
We have an input array:
int[] arrnum = {57, 38, 2, 14, 4, 2, 14};
If you want to find the duplicate numbers that appear more than once in an array list.
2appears twice14appears twice
So, our expected output is:
Duplicate items are:
2
14
This C# program utilizes lists and loops to solve the problem:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int[] arrnum = {57, 38, 2, 14, 4, 2, 14};
List<int> unique = new List<int>();
List<int> duplicates = new List<int>();
foreach (var item in arrnum)
{
// If the item is already in 'unique', it's a duplicate number
if (unique.Contains(item))
{
if(!duplicates.Contains(item))
duplicates.Add(item);
}
else
{
unique.Add(item);
}
}
// Print duplicates
Console.WriteLine("Duplicate items are:");
foreach (var duplicateItem in duplicates)
{
Console.WriteLine(duplicateItem);
}
}
}
Step-by-Step Explanation of the Code
Let’s carefully break this down:
1. Input Array
int[] arrnum = {57, 38, 2, 14, 4, 2, 14};
This is our dataset. Out of these numbers, 2 and 14 occur more than once.
2. Helper Lists
List<int> unique = new List<int>();
List<int> duplicates = new List<int>();
We maintain two lists:
unique: Keeps track of numbers we see for the first time.duplicates: Keeps track of numbers that are repeated.
Think of unique as a memory of “already visited numbers,” and duplicates as a final list of “problematic repeated numbers.”
3. Loop Through Each Element
foreach (var item in arrnum)
{
if (unique.Contains(item))
{
if(!duplicates.Contains(item))
duplicates.Add(item);
}
else
{
unique.Add(item);
}
}
Here’s what happens step by step for each number:
- First item → 57
- Not in
unique→ Add it. unique = {57}, duplicates = {}
- Not in
- Second item → 38
- Not in
unique→ Add it. unique = {57, 38}, duplicates = {}
- Not in
- Third item → 2
- Not in
unique→ Add it. unique = {57, 38, 2}, duplicates = {}
- Not in
- Fourth item → 14
- Not in
unique→ Add it. unique = {57, 38, 2, 14}, duplicates = {}
- Not in
- Fifth item → 4
- Not in
unique→ Add it. unique = {57, 38, 2, 14, 4}, duplicates = {}
- Not in
- Sixth item → 2
- Already in
unique→ Add toduplicates. unique = {57, 38, 2, 14, 4}, duplicates = {2}
- Already in
- Seventh item → 14
- Already in
unique→ Add toduplicates. unique = {57, 38, 2, 14, 4}, duplicates = {2, 14}
- Already in
Loop ends. We now have:
unique= {57, 38, 2, 14, 4}duplicates= {2, 14}
4. Printing Results
Console.WriteLine("Duplicate items are:");
foreach (var duplicateItem in duplicates)
{
Console.WriteLine(duplicateItem);
}
This prints:
Duplicate items are:
2
14
Why Does This Work?
The logic here is simple yet powerful:
- Track everything you see once in
unique. - The duplicates variable should be used if a number appears more than once.
- Use
Containschecks to avoid adding the same number multiple times in duplicates.
This way, the output always contains only the distinct repeating numbers.
Real-Life Analogy
Imagine you’re collecting tokens from a box:
- Every time you see a new token, you keep it aside (that’s your
uniquelist). - If you come across the same token again, you say, “Ah! I already have this one!” and put it into another pile (
duplicates). - By the end, your second pile has only the tokens that repeated at least once.
Final Output
Running this program with {57, 38, 2, 14, 4, 2, 14} produces:
Duplicate items are:
2
14
Key Points to Remember
- Use a
uniquelist to track first-time numbers. - Use a
duplicateslist to store repeating numbers. - Ensure duplicates are stored only once by checking
if(!duplicates.Contains(item)). - Time complexity can grow if the array is large since
Containschecks each element in the list. For very large datasets,HashSetmight be more efficient.