Finding Distinct Elements In A Data Structure Using C#

In computer science, while coding, it is one thing to learn the language. Learning the language involves remembering the basic syntax and forming a “Hello World” code. After that comes solving problems. A good programmer is one who can solve a problem by multiple methods, pun intended. Some of them are easy to understand but tardy and time-consuming while others are simple two-liners but with an implementation that is very difficult to comprehend.
 
At the end of the day, our aim is to solve a problem and find the simplest way to do it, but in order to reach that level, one has to do it through the long way. Each difficult problem can be divided into sub-problems. Finding distinct objects is also a kind of sub-problem that we usually come across while solving programming questions in placement exams.
 
C# provides a very sleek feature for this functionality, which we will discuss later. Firstly solving this issue with the tedious approach will help us understand coding at a deeper level.
 

The Long Way

 
So, when removing duplicates we assume that we have an empty box and we go through all the items in the group sequentially. There is one condition to enter the box, that is if the box has anything like you, it won’t let you in. Iterating this alogrithm will simply provide us all the distinct elements in any data structure.
 
For Numbers,
  1. using System;  
  2. using System.Collections;  
  3.       
  4. public class Program{      
  5.     public static void Main(){      
  6.         int[] arr = {1,3,1,4,7,4,2,1,5,5,3};  
  7.           
  8.     //Find only Unique Elements  
  9.           
  10.         //Declaring the Empty Box  
  11.         ArrayList sol = new ArrayList();  
  12.           
  13.           
  14.         for(int i=0;i<arr.Length;i++){  
  15.             //Mandatory Entry Condition  
  16.             if(sol.Contains(arr[i])==false){  
  17.                 sol.Add(arr[i]);  
  18.             }  
  19.         }  
  20.           
  21.         //ONLY FOR DISPLAYING OUTPUT  
  22.          Console.Write("Original Array : ");  
  23.           
  24.         foreach(var a in arr){  
  25.             Console.Write("{0} ",a);  
  26.         }  
  27.           
  28.         Console.Write("\n");  
  29.           
  30.         Console.Write("Modified Array : ");  
  31.         //Checking the Output  
  32.         foreach(var j in sol){  
  33.             Console.Write(j+ " ");  
  34.         }  
  35.     }        
  36. }      
Note
I used ArrayList as the Empty box data type to avoid the need to declare the initial size of the array, as ArrayList is dynamic in that form.
 
OUTPUT
 
Finding Distinct Elements in a Data Structure using C#
 
For String or Characters,
  1. using System;  
  2. using System.Text;  
  3.       
  4. public class Program{      
  5.     public static void Main(){      
  6.         String s = "aasjnajsnascnsdchhede";  
  7.           
  8.         //Declaring Empty Box  
  9.         StringBuilder sb = new StringBuilder();  
  10.          
  11.        foreach(char c in s){  
  12.            //Checking at the Entry  
  13.            if(sb.ToString().Contains(c.ToString())==false){  
  14.                sb.Append(c.ToString());  
  15.            }  
  16.        }  
  17.          
  18.        //Our Modified Output  
  19.        Console.Write(sb.ToString());  
  20.     }        
  21. }       
Note
We used StringBuilder data type to insinuate that even the data types that do not support function Contains() can be manipulated to perform according to our requirements.
 
OUTPUT
 
Finding Distinct Elements in a Data Structure using C#
 

The Short Way

 
Even though the above-mentioned examples seem short, Appling the box algorithm and manipulating data in competitive programming become an overhead, both for memory and time. In C# we can avoid all the hassle by simply removing duplicate items using the Distinct() method. This implementation is straightforward and thus very easy to remember.
  1. using System;  
  2. using System.Linq;  
  3.       
  4. public class Program{      
  5.     public static void Main(){      
  6.         int[] arr = {1,2,1,2,2,3,4,2,5,7,5,1};  
  7.           
  8.         var q = arr.Distinct();  
  9.         foreach(var v in q){  
  10.           Console.Write(v + " ");  
  11.         }   
  12.     }        
  13. }      
OUTPUT 
 
Finding Distinct Elements in a Data Structure using C#