Get Distinct values of an Array using LINQ

In this blog post, I will show how you can filter your array and remove all duplicate elements so that the end result will be array with distinct values. Moreover, the main idea is to do this without using any other (two or more) array(s) to make this work.

First of all, I will show two solutions without using LINQ, so that you will see the real difference, without and with LINQ.

 

FIRST SOLUTION
In this solution, I’m using two arrays, one first and second final array. For each item in the first array, I’m checking if final array contains such item. If not, add it to final array else just go to the next item of the first array.

CODE

string[] myFirstArray = {"Skopje", "Berlin", "Skopje", "London", "New York", "Roma", "Roma" }; //some random items
string[] finalArray = new string[myFirstArray.Length]; //finalArray with size equal to the first array

int c = 0;
for (int i = 0; i < myFirstArray.Length; i++)
{
    if (!(finalArray.Contains(myFirstArray[i])))
    {
        finalArray[c] = myFirstArray[i];                    
        c++;
    }
}

The result, finalArray will have the following values:


As you see, item 5 and 6 have null values, because after removing the duplicates, we have new final array with two less items than the first array.

Note: finalArray.Contains<> method is LINQ‘s method. However, this blog concerns more about filtering arrays with Distinct values. So, we should not care much about this implementation here. The Contains() method also exists in the ArrayList generic array.

 

SECOND SOLUTION
In the second solution, I’m using only one array. However, I need to do two loops with one inner loop where I go through all items that were already passed with the outer loop in the array and compare each with the current item from the outer loop.

CODE

string[] myArray = { "Skopje", "Berlin", "Skopje", "London", "New York", "Roma", "Roma" };
for (int i = 0; i < myArray.Length; i++)//first loop
{
    //second loop if j is less then i, then check if there is an item
    //with equal value within the items that were previously passed with the outer loop in the array
    for (int j = 0; j < i; j++)
    {
        if (myArray[i] == myArray[j])
        {
            myArray[i] = null;
        }
    }
}

The result, myArray will have the following values:


In this case, again we have two null values. Not the last ones, but these where we’ve set manually to null when duplicate found. We can not do otherwise because the arrays are a fixed size data structure. In our case the myArray is same as we have created 7 strings with the given values. To remove the duplicates, we can either set the item to null or String.Empty (for strings).

 

THIRD SOLUTION  - USING LINQ
This is definitely the finest solution, comparing with the previous two. You can see the difference. With two code lines, we are doing everything and plus we have no empty strings or null values in the final array.

CODE

string[] myArray = { "Skopje", "Berlin", "Skopje", "London", "New York", "Roma", "Roma" };
myArray = myArray.Distinct().ToArray();

So, using Distinct().ToArray(), we can easily get the distinct values and convert them to array.

See the result


Try it and give me your feedback.

Kind Regards,
Hajan

5 Comments

Comments have been disabled for this content.