
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Remove Duplicates from an Array
In this article, we are going to check how we can remove duplicate elements from an array in C# using different approaches.
What are Duplicate Elements in an Array?
The elements which exist more than once in a given array are duplicate elements. In this problem, we are given an array, and we have to remove duplicate elements and print unique elements.
Examples
Input: array = {1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6} Output:
array = {1, 2, 3, 4, 5} Explanation: After removing the duplicate elements, the remaining elements are 1, 2, 3, 4, and 5.
Input: array = {11, 13, 11, 15, 16, 14, 12, 12, 14, 16, 13, 15} Output: array = {11, 12, 13, 14, 15, 16} Explanation: After removing the duplicate elements, the remaining elements are 11, 12, 13, 14, 15, and 16.
Input: array = {} Output: array = {} Explanation: As the array is empty, no element is present.
Input: array = {-1, -1, -2, -3, -3, -4, -4, -4, -5, -5, -6, -6} Output: array = {-1, -2, -3, -4, -5} Explanation: After removing the duplicate elements, the remaining elements are -1, -2, -3, -4, and -5.
Remove Duplicates from Array Using HashSet
This is the most simple, easy, and straightforward approach. We will use a HashSet (a collection that only stores unique elements) to store all the elements of the array. The HashSet will automatically remove all duplicate elements from the array as we add elements to it. Finally, we convert the HashSet back into the array and print the unique elements.
Steps for Implementation
- Take an empty HashSet to store all the elements.
- HashSet stores only unique elements and removes duplicates.
- Add all elements to the HashSet.
- Convert the HashSet back into the array.
Implementation Code
using System; using System.Collections.Generic; using System.Linq; // Required for the ToArray method class Program { static void Main() { int[] array = { 1, 2, 2, 3, 4, 4, 4, 5 }; HashSet<int> uniqueElements = new HashSet<int>(); foreach (int num in array) { uniqueElements.Add(num); } int[] resultArray = uniqueElements.ToArray(); // Converts HashSet to array Console.WriteLine("Array after removing duplicate elements: " + string.Join(", ", resultArray)); } }
Output
Array after removing duplicate elements: 1, 2, 3, 4, 5
Time Complexity: O(n), traversing through elements of the array.
Space Complexity: O(n), using HashSet to store elements.
Remove Duplicates from Array Using Sorting
In this approach, we first sort the array. When sorted, duplicate elements will be next to each other. We then iterate through the array, adding elements that are different from the previous element to a new list. This approach increases time complexity due to sorting.
Steps for Implementation
- First, sort the array.
- Iterate through the array and add elements that are different from their previous element to a new list.
- Convert the list back to an array.
- Print all unique elements.
Implementation Code
using System; using System.Collections.Generic; class Program { static void Main() { int[] array = { 1, 2, 2, 3, 4, 4, 4, 5 }; Array.Sort(array); List<int> uniqueElements = new List<int>(); uniqueElements.Add(array[0]); for (int i = 1; i < array.Length; i++) { if (array[i] != array[i - 1]) { uniqueElements.Add(array[i]); } } int[] resultArray = uniqueElements.ToArray(); Console.WriteLine("Array after removing duplicate elements: " + string.Join(", ", resultArray)); } }
Output
Array after removing duplicate elements: 1, 2, 3, 4, 5
Time Complexity: O(n log n), due to sorting.
Space Complexity: O(n), using a new list to store unique elements.
Comparisons Between Both Approaches
The HashSet approach is faster for unsorted data.
The sorting approach is useful when the array needs to remain sorted.