Developer insights C#

Sorting an Array and Finding Duplicate Numbers in C# (Step-by-Step Guide)

Sorting an Array and Finding Duplicate Numbers in C#

Working with arrays is one of the most fundamental tasks in programming. In many real-world applications, you may need to sort an array, identify duplicate values, and extract useful insights like the second largest number.

In this blog, we’ll walk through a C# program that does all of this in one go:

  • Sorts the array using a simple algorithm
  • Finds duplicate numbers
  • Prints unique items
  • Finds the second largest element

Let’s go step by step.

The Program

Here’s the full code:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        int[] arrnum = {12, 13, 14, 14, 133, 13, 13, 13, 14, 166, 133, 155, 166, 12, 11, 15};
        int arrLen = arrnum.Length;

        List<int> unique = new List<int>();
        List<int> duplicates = new List<int>();
        
        // Bubble Sort implementation
        for (int index = 0; index < arrLen - 1; index++)
        {
            for (int j = 0; j < arrLen - index - 1; j++)
            {
                if (arrnum[j] > arrnum[j + 1])
                {
                    int temp = arrnum[j];
                    arrnum[j] = arrnum[j + 1];
                    arrnum[j + 1] = temp;
                }
            }
        }
		
        // Display the sorted array and find duplicates
        Console.Write("Sorting Array Are : ");
        foreach (var item in arrnum)
        {	
            Console.Write(item + " ");

            if (unique.Contains(item))
            {
                if(!duplicates.Contains(item))
                    duplicates.Add(item);
            }
            else
            {
                unique.Add(item);
            }
        }

        Console.WriteLine("\n\nUnique items are:");
        foreach (var uniqueItem in unique)
        {
            Console.Write(uniqueItem + " ");
        }
		
        Console.WriteLine("\n\n2nd Largest items are:");
        Console.Write(unique[unique.Count - 2]);        

        Console.WriteLine("\n\nDuplicate items are:");
        foreach (var duplicateItem in duplicates)
        {
            Console.WriteLine(duplicateItem);
        }
    }
}

Step-by-Step Explanation

1. Input Array

int[] arrnum = {12, 13, 14, 14, 133, 13, 13, 13, 14, 166, 133, 155, 166, 12, 11, 15};

We start with an array that has several repeating numbers. For example, 12, 13, 14, 133, and 166 all occur more than once.


2. Sorting the Array (Bubble Sort)

for (int index = 0; index < arrLen - 1; index++)
{
    for (int j = 0; j < arrLen - index - 1; j++)
    {
        if (arrnum[j] > arrnum[j + 1])
        {
            int temp = arrnum[j];
            arrnum[j] = arrnum[j + 1];
            arrnum[j + 1] = temp;
        }
    }
}

Here, we are using Bubble Sort, a classic algorithm where larger numbers “bubble up” to the end of the array after each iteration.

Example of how Bubble Sort works on the first few steps:

  • Compare between 12 and 13already sorted, no swap.
  • Compare between 13 and 14no swap.
  • Compare between 14 and 14no swap.
  • Compare between 14 and 133no swap.
  • Compare between 133 and 13swap!

After repeating these steps, the array becomes fully sorted:

Sorting array data are: 11 12 12 13 13 13 13 14 14 14 15 133 133 155 166 166

3. Identifying Unique and Duplicate Numbers

We keep track of numbers using two lists:

List<int> unique = new List<int>();
List<int> duplicates = new List<int>();

As we print the sorted numbers, we check:

  • Add the specific number to a unique variable if it’s new.
  • If the number already exists in unique, move it into duplicates (but only once).

This ensures duplicates contains only distinct repeating numbers.

After processing, we get:

Unique items are:
11 12 13 14 15 133 155 166

4. Finding the Second Largest Number

Console.WriteLine("\n\n2nd Largest items are:");
Console.Write(unique[unique.Count - 2]);

Since our unique number list is sorted, the second largest element will always be at unique.Count - 2.

In our case:

  • Largest element = 166
  • Second largest = 155

So, output is:

2nd Largest items are:
155

5. Printing Duplicate Numbers

Finally, we print the duplicates:

Console.WriteLine("\n\nDuplicate items are:");
foreach (var duplicateItem in duplicates)
{
    Console.WriteLine(duplicateItem);
}

Output:

Duplicate items are:
12
13
14
133
166

Final Program Output

The result of after running the entire program is:

Sorting array data are : 11 12 12 13 13 13 13 14 14 14 15 133 133 155 166 166 

Unique items are:
11 12 13 14 15 133 155 166 

2nd Largest items are:
155

Duplicate items are:
12
13
14
133
166

Why This Code is Useful

This program is a great all-in-one practice for beginners in C#:

  • Sorting (Bubble Sort logic)
  • Data validation (detecting duplicates)
  • Extracting key insights (finding second largest element)
  • Understanding arrays and lists together

In real-world projects, such tasks are common when cleaning raw data, analyzing user records, or preparing input before saving into databases.


Key Takeaways

  1. Bubble Sort is easy to implement, though not the most efficient for large datasets.
  2. Using two lists (unique and duplicates) helps separate clean data from repeating data.
  3. The second largest integer can be found by accessing the second-last entry in a sorted, unique list. 
  4. The program combines sorting, duplicate detection, and value extraction neatly.

Leave a Reply

Your email address will not be published. Required fields are marked *