Unique Array Items In C#

Introduction

In this article, we will discuss about unique array items in C# using a traditional approach, LINQ, and non-case sensitive unique array items in LINQ.

Traditional Approach

Unique array items can be displayed by using for loop and checking the existence of duplicate items in the array. Here is the code to display unique array items using a traditional approach. Let’s take array 2, 3, 5, 3, 7, 5.

  1. static void Main(string[] args)  
  2.         {  
  3.             int[] items = { 2, 3, 5, 3, 7, 5 };  
  4.             int n = items.Length;  
  5.   
  6.             Console.WriteLine("Unique array elements: ");  
  7.   
  8.             for(int i=0; i<n;i++)  
  9.             {  
  10.                 bool isDuplicate = false;  
  11.                 for(int j=0;j<i;j++)  
  12.                 {  
  13.                     if(items[i] == items[j])  
  14.                     {  
  15.                         isDuplicate = true;  
  16.                         break;  
  17.                     }  
  18.                 }  
  19.   
  20.                 if(!isDuplicate)  
  21.                 {  
  22.                     Console.WriteLine(items[i]);  
  23.                       
  24.                 }  
  25.             }  
  26.   
  27.             Console.ReadLine();  
  28.         }  

Output

Output

LINQ Approach

To use LINQ import namespace System.Linq. There is a Distinct() method in Linq which gives unique items.

  1. static void Main(string[] args)  
  2.         {  
  3.             int[] items = { 2, 3, 5, 3, 7, 5 };  
  4.             IEnumerable<int> uniqueItems = items.Distinct<int>();  
  5.             Console.WriteLine("Unique array elements using LINQ: " + string.Join(",", uniqueItems));  
  6.   
  7.             Console.ReadLine();  
  8.         }  

Output

Output

Unique array items without case sensitive using LINQ in C#

In SQL if we have to get unique items, distinct keyword gives unique non case sensitive items because SQL is not a case sensitive language. So it’s not as easy to use in LINQ because C# is a case sensitive language. Let’s take a list of array items: AAA, Bbb, Aaa, bbb, CCC, Ddd, aaa, CCC. There are 8 items here, out output should be like Aaa, Bbb, Ccc, Ddd.

To display items in title case we need to use TextInfo class. In order to use textInfo class import System.Globalization class.

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             string[] items = { "AAA""Bbb""Aaa""bbb""CCC""Ddd""aaa""CCC" };  
  6.               
  7.             IEnumerable<string> uniqueItems = items.Select(x => x.ToLower()).Distinct<string>();  
  8.             TextInfo textInfo = new CultureInfo("en-US"false).TextInfo;  
  9.             uniqueItems = uniqueItems.Select(x => textInfo.ToTitleCase(x));  
  10.             Console.WriteLine("Unique array elements using LINQ without case sensitive: " + string.Join(",", uniqueItems));  
  11.   
  12.             Console.ReadLine();  
  13.         }  
  14.     }  

Output

Output

Conclusion

In this blog, we got unique array items using For loop and LINQ. Also, we displayed  non-case sensitive items  using LINQ and TextInfo class.