Tech Point Fundamentals

Thursday, January 13, 2022

C# Program to Remove Duplicate Elements from Array

C# Program to Remove Duplicate Elements from Array


Most of the IT companies check the coding skills and problem-solving skills as well along with the theoretical interview questions. Sometimes you are free to write the pseudo code and sometimes you are asked to write the complete program either on any paper or any editor. 

This question is asked in the coding interview to write the program. Here you can find the program as well as a live running program so that you can test the program immediately.

Watch our videos here


How can you remove the duplicate elements from an integer array without using any library function? Write a program for the same.

Removing duplicate values from an array is basically getting distinct values. In C#, we cannot remove values in the array directly. So, we have to get the distinct values from the specified array and create a new array of distinct values instead of removing duplicate values. 

One way to achieve the same is using HashKey. Since HashSet discards the duplicates, we can convert the given array (with duplicates) to a HashSet, and then convert the HashSet back to the array. By doing so the duplicate elements will be removed but the disadvantage is that the order of elements may be changed. 

C# Program to Remove the Duplicate Elements from an Array


// Author 	: Tech Point Fundamentals
// Website 	:
// Channel	:
// Demo		: Removing Dupliate Array Elements 

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
 public static void Main()
	int[] unsortedArray = new int[8] {5, 3, 6, 2, 1, 3, 8, 5};			
	Console.WriteLine("UnSorted Array With Duplicate Elements: ");	
	Console.WriteLine("------------------------------------------ ");	
	for( int i = 0; i < unsortedArray.Length; i++)
	// Removing Duplicates using User Defined Function
	var distinctArrayByHash = RemoveArrayDuplicates(unsortedArray);
	Console.WriteLine("\nDistinct Array Using HashSet: ");	
	Console.WriteLine("------------------------------------------ ");	
	for( int i = 0; i <= distinctArrayByHash.Length - 1; i++)
	// Removing Duplicates using LINQ Library Function
	 var distinctArray = unsortedArray.Distinct().ToArray();
	Console.WriteLine("\nDistinct Array Using LINQ: ");	
	Console.WriteLine("------------------------------------------ ");	
	for( int i = 0; i <= distinctArray.Length - 1; i++)
public static T[] RemoveArrayDuplicates<T>(T[] array)
	HashSet<T> set = new HashSet<T>(array);
	T[] result = new T[set.Count];
	return result;


                UnSorted Array With Duplicate Elements: ------------------------------------------ 5 3 6 2 1 3 8 5 Distinct Array Using HashSet: ------------------------------------------ 5 3 6 2 1 8 Distinct Array Using LINQ: ------------------------------------------ 5 3 6 2 1 8

Live Demo

No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.