<p>I have been working with a <code>string[]</code> array in C# that gets returned from a function call. I could possibly cast to a <code>Generic</code> collection, but I was wondering if there was a better way to do it, possibly by using a temp array.</p>

<p><strong>What is the best way to remove duplicates from a C# array?</strong></p>

[{'answer_id': 9677, 'author': 'Matthew Schinckel', 'author_id': 188, 'author_profile': 'https://Stackoverflow.com/users/188', 'pm_score': 4, 'selected': False, 'text': '<p>If you needed to sort it, then you could implement a sort that also removes duplicates.</p>\n\n<p>Kills two birds with one stone, then.</p>\n'}, {'answer_id': 9685, 'author': 'Jeff Atwood', 'author_id': 1, 'author_profile': 'https://Stackoverflow.com/users/1', 'pm_score': 10, 'selected': True, 'text': '<p>You could possibly use a LINQ query to do this:</p>\n\n<pre><code>int[] s = { 1, 2, 3, 3, 4};\nint[] q = s.Distinct().ToArray();\n</code></pre>\n'}, {'answer_id': 9735, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 3, 'selected': False, 'text': '<p>Add all the strings to a dictionary and get the Keys property afterwards. This will produce each unique string, but not necessarily in the same order your original input had them in.</p>\n\n<p>If you require the end result to have the same order as the original input, when you consider the first occurance of each string, use the following algorithm instead:</p>\n\n<ol>\n<li>Have a list (final output) and a dictionary (to check for duplicates)</li>\n<li>For each string in the input, check if it exists in the dictionary already</li>\n<li>If not, add it both to the dictionary and to the list</li>\n</ol>\n\n<p>At the end, the list contains the first occurance of each unique string.</p>\n\n<p>Make sure you consider things like culture and such when constructing your dictionary, to make sure you handle duplicates with accented letters correctly.</p>\n'}, {'answer_id': 9755, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 4, 'selected': False, 'text': '<p>The following tested and working code will remove duplicates from an array. You must include the System.Collections namespace.</p>\n\n<pre><code>string[] sArray = {"a", "b", "b", "c", "c", "d", "e", "f", "f"};\nvar sList = new ArrayList();\n\nfor (int i = 0; i &lt; sArray.Length; i++) {\n    if (sList.Contains(sArray[i]) == false) {\n        sList.Add(sArray[i]);\n    }\n}\n\nvar sNew = sList.ToArray();\n\nfor (int i = 0; i &lt; sNew.Length; i++) {\n    Console.Write(sNew[i]);\n}\n</code></pre>\n\n<p>You could wrap this up into a function if you wanted to.</p>\n'}, {'answer_id': 9763, 'author': 'rjzii', 'author_id': 1185, 'author_profile': 'https://Stackoverflow.com/users/1185', 'pm_score': 3, 'selected': False, 'text': "<p>This might depend on how much you want to engineer the solution - if the array is never going to be that big and you don't care about sorting the list you might want to try something similar to the following:</p>\n\n<pre><code>    public string[] RemoveDuplicates(string[] myList) {\n        System.Collections.ArrayList newList = new System.Collections.ArrayList();\n\n        foreach (string str in myList)\n            if (!newList.Contains(str))\n                newList.Add(str);\n        return (string[])newList.ToArray(typeof(string));\n    }\n</code></pre>\n"}, {'answer_id': 9768, 'author': 'ZombieSheep', 'author_id': 377, 'author_profile': 'https://Stackoverflow.com/users/377', 'pm_score': 2, 'selected': False, 'text': '<p>NOTE : NOT tested!</p>\n\n<pre><code>string[] test(string[] myStringArray)\n{\n    List&lt;String&gt; myStringList = new List&lt;string&gt;();\n    foreach (string s in myStringArray)\n    {\n        if (!myStringList.Contains(s))\n        {\n            myStringList.Add(s);\n        }\n    }\n    return myStringList.ToString();\n}\n</code></pre>\n\n<p>Might do what you need...</p>\n\n<p><strong>EDIT</strong> Argh!!! beaten to it by rob by under a minute!</p>\n'}, {'answer_id': 9833, 'author': 'Arcturus', 'author_id': 900, 'author_profile': 'https://Stackoverflow.com/users/900', 'pm_score': 6, 'selected': False, 'text': '<p>Here is the <a href="http://msdn.microsoft.com/en-us/library/bb359438.aspx" rel="noreferrer">HashSet&lt;string&gt;</a> approach:</p>\n\n<pre><code>public static string[] RemoveDuplicates(string[] s)\n{\n    HashSet&lt;string&gt; set = new HashSet&lt;string&gt;(s);\n    string[] result = new string[set.Count];\n    set.CopyTo(result);\n    return result;\n}\n</code></pre>\n\n<p>Unfortunately this solution also requires .NET framework 3.5 or later as HashSet was not added until that version. You could also use <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.distinct.aspx" rel="noreferrer">array.Distinct()</a>, which is a feature of LINQ.</p>\n'}, {'answer_id': 9873, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 3, 'selected': False, 'text': "<blockquote>\n<pre><code>List&lt;String&gt; myStringList = new List&lt;string&gt;();\nforeach (string s in myStringArray)\n{\n    if (!myStringList.Contains(s))\n    {\n        myStringList.Add(s);\n    }\n}\n</code></pre>\n</blockquote>\n\n<p>This is <strong>O(n^2)</strong>, which won't matter for a short list which is going to be stuffed into a combo, but could be rapidly be a problem on a big collection.</p>\n"}, {'answer_id': 1856035, 'author': 'Sesh', 'author_id': 38212, 'author_profile': 'https://Stackoverflow.com/users/38212', 'pm_score': 3, 'selected': False, 'text': "<p>Here is a <strong>O(n*n)</strong> approach that uses <strong>O(1)</strong> space. </p>\n\n<pre><code>void removeDuplicates(char* strIn)\n{\n    int numDups = 0, prevIndex = 0;\n    if(NULL != strIn &amp;&amp; *strIn != '\\0')\n    {\n        int len = strlen(strIn);\n        for(int i = 0; i &lt; len; i++)\n        {\n            bool foundDup = false;\n            for(int j = 0; j &lt; i; j++)\n            {\n                if(strIn[j] == strIn[i])\n                {\n                    foundDup = true;\n                    numDups++;\n                    break;\n                }\n            }\n\n            if(foundDup == false)\n            {\n                strIn[prevIndex] = strIn[i];\n                prevIndex++;\n            }\n        }\n\n        strIn[len-numDups] = '\\0';\n    }\n}\n</code></pre>\n\n<p>The <strong>hash/linq</strong> approaches above are what you would generally use in real life. However in interviews they usually want to put some constraints e.g. constant space which rules out hash or no internal <strong>api</strong> - which rules out using <strong>LINQ</strong>.</p>\n"}, {'answer_id': 2644115, 'author': 'Vijay Swami', 'author_id': 317324, 'author_profile': 'https://Stackoverflow.com/users/317324', 'pm_score': 3, 'selected': False, 'text': '<p>The following piece of code attempts to remove duplicates from an ArrayList though this is not an optimal solution. I was asked this question during an interview to remove duplicates through recursion, and without using a second/temp arraylist:  </p>\n\n<pre><code>private void RemoveDuplicate() \n{\n\nArrayList dataArray = new ArrayList(5);\n\n            dataArray.Add("1");\n            dataArray.Add("1");\n            dataArray.Add("6");\n            dataArray.Add("6");\n            dataArray.Add("6");\n            dataArray.Add("3");\n            dataArray.Add("6");\n            dataArray.Add("4");\n            dataArray.Add("5");\n            dataArray.Add("4");\n            dataArray.Add("1");\n\n            dataArray.Sort();\n\n            GetDistinctArrayList(dataArray, 0);\n}\n\nprivate void GetDistinctArrayList(ArrayList arr, int idx)\n\n{\n\n            int count = 0;\n\n            if (idx &gt;= arr.Count) return;\n\n            string val = arr[idx].ToString();\n            foreach (String s in arr)\n            {\n                if (s.Equals(arr[idx]))\n                {\n                    count++;\n                }\n            }\n\n            if (count &gt; 1)\n            {\n                arr.Remove(val);\n                GetDistinctArrayList(arr, idx);\n            }\n            else\n            {\n                idx += 1;\n                GetDistinctArrayList(arr, idx);\n            }\n        }\n</code></pre>\n'}, {'answer_id': 9272477, 'author': 'Pintu', 'author_id': 1177685, 'author_profile': 'https://Stackoverflow.com/users/1177685', 'pm_score': 3, 'selected': False, 'text': '<pre><code>protected void Page_Load(object sender, EventArgs e)\n{\n    string a = "a;b;c;d;e;v";\n    string[] b = a.Split(\';\');\n    string[] c = b.Distinct().ToArray();\n\n    if (b.Length != c.Length)\n    {\n        for (int i = 0; i &lt; b.Length; i++)\n        {\n            try\n            {\n                if (b[i].ToString() != c[i].ToString())\n                {\n                    Response.Write("Found duplicate " + b[i].ToString());\n                    return;\n                }\n            }\n            catch (Exception ex)\n            {\n                Response.Write("Found duplicate " + b[i].ToString());\n                return;\n            }\n        }              \n    }\n    else\n    {\n        Response.Write("No duplicate ");\n    }\n}\n</code></pre>\n'}, {'answer_id': 13996868, 'author': 'komizo', 'author_id': 1792434, 'author_profile': 'https://Stackoverflow.com/users/1792434', 'pm_score': 3, 'selected': False, 'text': '<p>Maybe hashset which do  not  store  duplicate  elements  and  silently  ignore  requests  to  add\nduplicates.</p>\n\n<pre><code>static void Main()\n{\n    string textWithDuplicates = "aaabbcccggg";     \n\n    Console.WriteLine(textWithDuplicates.Count());  \n    var letters = new HashSet&lt;char&gt;(textWithDuplicates);\n    Console.WriteLine(letters.Count());\n\n    foreach (char c in letters) Console.Write(c);\n    Console.WriteLine("");\n\n    int[] array = new int[] { 12, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2 };\n\n    Console.WriteLine(array.Count());\n    var distinctArray = new HashSet&lt;int&gt;(array);\n    Console.WriteLine(distinctArray.Count());\n\n    foreach (int i in distinctArray) Console.Write(i + ",");\n}\n</code></pre>\n'}, {'answer_id': 17121191, 'author': 'AptSenSDET', 'author_id': 2488362, 'author_profile': 'https://Stackoverflow.com/users/2488362', 'pm_score': 2, 'selected': False, 'text': '<p>Tested the below &amp; it works. What\'s cool is that it does a culture sensitive search too</p>\n\n<pre><code>class RemoveDuplicatesInString\n{\n    public static String RemoveDups(String origString)\n    {\n        String outString = null;\n        int readIndex = 0;\n        CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;\n\n\n        if(String.IsNullOrEmpty(origString))\n        {\n            return outString;\n        }\n\n        foreach (var ch in origString)\n        {\n            if (readIndex == 0)\n            {\n                outString = String.Concat(ch);\n                readIndex++;\n                continue;\n            }\n\n            if (ci.IndexOf(origString, ch.ToString().ToLower(), 0, readIndex) == -1)\n            {\n                //Unique char as this char wasn\'t found earlier.\n                outString = String.Concat(outString, ch);                   \n            }\n\n            readIndex++;\n\n        }\n\n\n        return outString;\n    }\n\n\n    static void Main(string[] args)\n    {\n        String inputString = "aAbcefc";\n        String outputString;\n\n        outputString = RemoveDups(inputString);\n\n        Console.WriteLine(outputString);\n    }\n</code></pre>\n\n<p>}</p>\n\n<p>--AptSenSDET</p>\n'}, {'answer_id': 17442504, 'author': 'Muhammad Mubashir', 'author_id': 878106, 'author_profile': 'https://Stackoverflow.com/users/878106', 'pm_score': 3, 'selected': False, 'text': '<p>-- This is <strong>Interview Question</strong> asked every time. Now i done its coding.</p>\n\n<pre><code>static void Main(string[] args)\n{    \n            int[] array = new int[] { 4, 8, 4, 1, 1, 4, 8 };            \n            int numDups = 0, prevIndex = 0;\n\n            for (int i = 0; i &lt; array.Length; i++)\n            {\n                bool foundDup = false;\n                for (int j = 0; j &lt; i; j++)\n                {\n                    if (array[i] == array[j])\n                    {\n                        foundDup = true;\n                        numDups++; // Increment means Count for Duplicate found in array.\n                        break;\n                    }                    \n                }\n\n                if (foundDup == false)\n                {\n                    array[prevIndex] = array[i];\n                    prevIndex++;\n                }\n            }\n\n            // Just Duplicate records replce by zero.\n            for (int k = 1; k &lt;= numDups; k++)\n            {               \n                array[array.Length - k] = \'\\0\';             \n            }\n\n\n            Console.WriteLine("Console program for Remove duplicates from array.");\n            Console.Read();\n        }\n</code></pre>\n'}, {'answer_id': 24587289, 'author': 'Salman Ramzan', 'author_id': 3807840, 'author_profile': 'https://Stackoverflow.com/users/3807840', 'pm_score': 2, 'selected': False, 'text': '<p>This code 100% remove duplicate values from an array[as I used a[i]].....You can convert it in any OO language..... :)</p>\n\n<pre><code>for(int i=0;i&lt;size;i++)\n{\n    for(int j=i+1;j&lt;size;j++)\n    {\n        if(a[i] == a[j])\n        {\n            for(int k=j;k&lt;size;k++)\n            {\n                 a[k]=a[k+1];\n            }\n            j--;\n            size--;\n        }\n    }\n\n}\n</code></pre>\n'}, {'answer_id': 29020414, 'author': 'Fábio Delboni', 'author_id': 4664826, 'author_profile': 'https://Stackoverflow.com/users/4664826', 'pm_score': 3, 'selected': False, 'text': '<p>Simple solution:</p>\n\n<pre><code>using System.Linq;\n...\n\npublic static int[] Distinct(int[] handles)\n{\n    return handles.ToList().Distinct().ToArray();\n}\n</code></pre>\n'}, {'answer_id': 29587789, 'author': 'R.Akhlaghi', 'author_id': 2830315, 'author_profile': 'https://Stackoverflow.com/users/2830315', 'pm_score': 1, 'selected': False, 'text': '<p>you can using This code when work with an ArrayList</p>\n\n<pre><code>ArrayList arrayList;\n//Add some Members :)\narrayList.Add("ali");\narrayList.Add("hadi");\narrayList.Add("ali");\n\n//Remove duplicates from array\n  for (int i = 0; i &lt; arrayList.Count; i++)\n    {\n       for (int j = i + 1; j &lt; arrayList.Count ; j++)\n           if (arrayList[i].ToString() == arrayList[j].ToString())\n                 arrayList.Remove(arrayList[j]);\n</code></pre>\n'}, {'answer_id': 35145623, 'author': 'HMartyrossian', 'author_id': 5075542, 'author_profile': 'https://Stackoverflow.com/users/5075542', 'pm_score': 1, 'selected': False, 'text': '<pre><code>public static int RemoveDuplicates(ref int[] array)\n{\n    int size = array.Length;\n\n    // if 0 or 1, return 0 or 1:\n    if (size  &lt; 2) {\n        return size;\n    }\n\n    int current = 0;\n    for (int candidate = 1; candidate &lt; size; ++candidate) {\n        if (array[current] != array[candidate]) {\n            array[++current] = array[candidate];\n        }\n    }\n\n    // index to count conversion:\n    return ++current;\n}\n</code></pre>\n'}, {'answer_id': 39868699, 'author': 'Papasani Mohansrinivas', 'author_id': 6504878, 'author_profile': 'https://Stackoverflow.com/users/6504878', 'pm_score': 1, 'selected': False, 'text': "<p>Below is an simple logic in java you traverse elements of array twice and if you see any same element you assign zero to it plus you don't touch the index of element you are comparing.</p>\n\n<pre><code>import java.util.*;\nclass removeDuplicate{\nint [] y ;\n\npublic removeDuplicate(int[] array){\n    y=array;\n\n    for(int b=0;b&lt;y.length;b++){\n        int temp = y[b];\n        for(int v=0;v&lt;y.length;v++){\n            if( b!=v &amp;&amp; temp==y[v]){\n                y[v]=0;\n            }\n        }\n    }\n}\n</code></pre>\n"}, {'answer_id': 47568568, 'author': 'Arie Yehieli', 'author_id': 4604796, 'author_profile': 'https://Stackoverflow.com/users/4604796', 'pm_score': 0, 'selected': False, 'text': '<pre><code>  private static string[] distinct(string[] inputArray)\n        {\n            bool alreadyExists;\n            string[] outputArray = new string[] {};\n\n            for (int i = 0; i &lt; inputArray.Length; i++)\n            {\n                alreadyExists = false;\n                for (int j = 0; j &lt; outputArray.Length; j++)\n                {\n                    if (inputArray[i] == outputArray[j])\n                        alreadyExists = true;\n                }\n                        if (alreadyExists==false)\n                        {\n                            Array.Resize&lt;string&gt;(ref outputArray, outputArray.Length + 1);\n                            outputArray[outputArray.Length-1] = inputArray[i];\n                        }\n            }\n            return outputArray;\n        }\n</code></pre>\n'}, {'answer_id': 52789546, 'author': 'Ali Bayat', 'author_id': 3427324, 'author_profile': 'https://Stackoverflow.com/users/3427324', 'pm_score': 2, 'selected': False, 'text': '<p>Generic Extension method :</p>\n\n<pre><code>public static IEnumerable&lt;TSource&gt; Distinct&lt;TSource&gt;(this IEnumerable&lt;TSource&gt; source, IEqualityComparer&lt;TSource&gt; comparer)\n{\n    if (source == null)\n        throw new ArgumentNullException(nameof(source));\n\n    HashSet&lt;TSource&gt; set = new HashSet&lt;TSource&gt;(comparer);\n    foreach (TSource item in source)\n    {\n        if (set.Add(item))\n        {\n            yield return item;\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 55854713, 'author': 'Rohan', 'author_id': 10154571, 'author_profile': 'https://Stackoverflow.com/users/10154571', 'pm_score': -1, 'selected': False, 'text': '<pre><code>using System;\nusing System.Collections.Generic;\nusing System.Linq;\n\n\nnamespace Rextester\n{\n    public class Program\n    {\n        public static void Main(string[] args)\n        {\n             List&lt;int&gt; listofint1 = new List&lt;int&gt; { 4, 8, 4, 1, 1, 4, 8 };\n           List&lt;int&gt; updatedlist= removeduplicate(listofint1);\n            foreach(int num in updatedlist)\n               Console.WriteLine(num);\n        }\n\n\n        public static List&lt;int&gt; removeduplicate(List&lt;int&gt; listofint)\n         {\n             List&lt;int&gt; listofintwithoutduplicate= new List&lt;int&gt;();\n\n\n              foreach(var num in listofint)\n                 {\n                  if(!listofintwithoutduplicate.Any(p=&gt;p==num))\n                        {\n                          listofintwithoutduplicate.Add(num);\n                        }\n                  }\n             return listofintwithoutduplicate;\n         }\n    }\n\n\n\n}\n</code></pre>\n'}, {'answer_id': 58223263, 'author': 'Kudakwashe Mafutah', 'author_id': 2917482, 'author_profile': 'https://Stackoverflow.com/users/2917482', 'pm_score': -1, 'selected': False, 'text': '<pre><code>strINvalues = "1,1,2,2,3,3,4,4";\nstrINvalues = string.Join(",", strINvalues .Split(\',\').Distinct().ToArray());\nDebug.Writeline(strINvalues);\n</code></pre>\n\n<p>Kkk Not sure if this is witchcraft or just beautiful code</p>\n\n<p><strong>1</strong> strINvalues .Split(\',\').Distinct().ToArray()</p>\n\n<p><strong>2</strong> string.Join(",", XXX);</p>\n\n<p><em>1</em> Splitting the array and using Distinct [LINQ] to remove duplicates\n<em>2</em> Joining it back without the duplicates.</p>\n\n<p>Sorry I never read the text on StackOverFlow just the code. it make more sense than the text ;) </p>\n'}, {'answer_id': 61983714, 'author': 'P_P', 'author_id': 1673752, 'author_profile': 'https://Stackoverflow.com/users/1673752', 'pm_score': 1, 'selected': False, 'text': '<p>The best way? Hard to say, the HashSet approach looks fast,\nbut (depending on the data) using a sort algorithm (CountSort ?)\ncan be much faster.</p>\n<pre><code>using System;\nusing System.Collections.Generic;\nusing System.Linq;\nclass Program\n{\n    static void Main()\n    {\n        Random r = new Random(0); int[] a, b = new int[1000000];\n        for (int i = b.Length - 1; i &gt;= 0; i--) b[i] = r.Next(b.Length);\n        a = new int[b.Length]; Array.Copy(b, a, b.Length);\n        a = dedup0(a); Console.WriteLine(a.Length);\n        a = new int[b.Length]; Array.Copy(b, a, b.Length);\n        var w = System.Diagnostics.Stopwatch.StartNew();\n        a = dedup0(a); Console.WriteLine(w.Elapsed); Console.Read();\n    }\n\n    static int[] dedup0(int[] a)  // 48 ms  \n    {\n        return new HashSet&lt;int&gt;(a).ToArray();\n    }\n\n    static int[] dedup1(int[] a)  // 68 ms\n    {\n        Array.Sort(a); int i = 0, j = 1, k = a.Length; if (k &lt; 2) return a;\n        while (j &lt; k) if (a[i] == a[j]) j++; else a[++i] = a[j++];\n        Array.Resize(ref a, i + 1); return a;\n    }\n\n    static int[] dedup2(int[] a)  //  8 ms\n    {\n        var b = new byte[a.Length]; int c = 0;\n        for (int i = 0; i &lt; a.Length; i++) \n            if (b[a[i]] == 0) { b[a[i]] = 1; c++; }\n        a = new int[c];\n        for (int j = 0, i = 0; i &lt; b.Length; i++) if (b[i] &gt; 0) a[j++] = i;\n        return a;\n    }\n}\n</code></pre>\n<p>Almost branch free. How? Debug mode, Step Into (F11) with a small array: {1,3,1,1,0}</p>\n<pre><code>    static int[] dedupf(int[] a)  //  4 ms\n    {\n        if (a.Length &lt; 2) return a;\n        var b = new byte[a.Length]; int c = 0, bi, ai, i, j;\n        for (i = 0; i &lt; a.Length; i++)\n        { ai = a[i]; bi = 1 ^ b[ai]; b[ai] |= (byte)bi; c += bi; }\n        a = new int[c]; i = 0; while (b[i] == 0) i++; a[0] = i++;\n        for (j = 0; i &lt; b.Length; i++) a[j += bi = b[i]] += bi * i; return a;\n    }\n</code></pre>\n<p>A solution with two nested loops might take some time,\nespecially for larger arrays.</p>\n<pre><code>    static int[] dedup(int[] a)\n    {\n        int i, j, k = a.Length - 1;\n        for (i = 0; i &lt; k; i++)\n            for (j = i + 1; j &lt;= k; j++) if (a[i] == a[j]) a[j--] = a[k--];\n        Array.Resize(ref a, k + 1); return a;\n    }\n</code></pre>\n'}, {'answer_id': 62239378, 'author': 'Swathi Sriramaneni', 'author_id': 13697010, 'author_profile': 'https://Stackoverflow.com/users/13697010', 'pm_score': 0, 'selected': False, 'text': '<pre><code>int size = a.Length;\n        for (int i = 0; i &lt; size; i++)\n        {\n            for (int j = i + 1; j &lt; size; j++)\n            {\n                if (a[i] == a[j])\n                {\n                    for (int k = j; k &lt; size; k++)\n                    {\n                        if (k != size - 1)\n                        {\n                            int temp = a[k];\n                            a[k] = a[k + 1];\n                            a[k + 1] = temp;\n\n                        }\n                    }\n                    j--;\n                    size--;\n                }\n            }\n        }\n</code></pre>\n'}, {'answer_id': 63166358, 'author': 'Sourcephy', 'author_id': 8196012, 'author_profile': 'https://Stackoverflow.com/users/8196012', 'pm_score': -1, 'selected': False, 'text': '<p>Removing duplicate and ignore case sensitive using Distinct &amp; StringComparer.InvariantCultureIgnoreCase</p>\n<pre><code>string[] array = new string[] { &quot;A&quot;, &quot;a&quot;, &quot;b&quot;, &quot;B&quot;, &quot;a&quot;, &quot;C&quot;, &quot;c&quot;, &quot;C&quot;, &quot;A&quot;, &quot;1&quot; };\nvar r = array.Distinct(StringComparer.InvariantCultureIgnoreCase).ToList();\nConsole.WriteLine(r.Count); // return 4 items\n</code></pre>\n'}, {'answer_id': 64519180, 'author': 'AKINNUBI ABIOLA SYLVESTER', 'author_id': 6596443, 'author_profile': 'https://Stackoverflow.com/users/6596443', 'pm_score': -1, 'selected': False, 'text': '<p>Find answer below.</p>\n<pre><code>class Program\n{\n    static void Main(string[] args)\n    {\n        var nums = new int[] { 1, 4, 3, 3, 3, 5, 5, 7, 7, 7, 7, 9, 9, 9 };\n        var result = removeDuplicates(nums);\n        foreach (var item in result)\n        {\n            Console.WriteLine(item);\n        }\n    }\n    static int[] removeDuplicates(int[] nums)\n    {\n        nums = nums.ToList().OrderBy(c =&gt; c).ToArray();\n        int j = 1;\n        int i = 0;\n        int stop = 0;\n        while (j &lt; nums.Length)\n        {\n            if (nums[i] != nums[j])\n            {\n                nums[i + 1] = nums[j];\n                stop = i + 2;\n                i++;\n            }\n            j++;\n        }\n        nums = nums.Take(stop).ToArray();\n        return nums;\n    }\n}\n</code></pre>\n<p>Just a bit of contribution based on a test i just solved, maybe helpful and open to improvement by other top contributors here.\nHere are the things i did:</p>\n<ol>\n<li>I used OrderBy which allows me order or sort the items from smallest to the highest using LINQ</li>\n<li>I then convert it to back to an array and then re-assign it back to the primary datasource</li>\n<li>So i then initialize j which is my right hand side of the array to be 1 and i which is my left hand side of the array to be 0, i also initialize where i would i to stop to be 0.</li>\n<li>I used a while loop to increment through the array by going from one position to the other left to right, for each increment the stop position is the current value of i + 2 which i will use later to truncate the duplicates from the array.</li>\n<li>I then increment by moving from left to right from the if statement and from right to right outside of the if statement until i iterate through the entire values of the array.</li>\n<li>I then pick from the first element to the stop position which becomes the last i index plus 2. that way i am able to remove all the duplicate items from the int array. which is then reassigned.</li>\n</ol>\n'}, {'answer_id': 68816360, 'author': 'Faisal Ansari', 'author_id': 6898760, 'author_profile': 'https://Stackoverflow.com/users/6898760', 'pm_score': 0, 'selected': False, 'text': '<p>So I was doing an interview session and got the same question to sort and distinct</p>\n<pre><code>static void Sort()\n    {\n        try\n        {\n            int[] number = new int[Convert.ToInt32(Console.ReadLine())];\n            for (int i = 0; i &lt; number.Length; i++)\n            {\n                number[i] = Convert.ToInt32(Console.ReadLine());\n            }\n            Array.Sort(number);\n            int[] num = number.Distinct().ToArray();\n            for (int i = 0; i &lt; num.Length; i++)\n            {\n                Console.WriteLine(num[i]);\n            }\n        }\n        catch (Exception ex)\n        {\n            Console.WriteLine(ex);\n        }\n        Console.Read();\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/9673', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/493/']