-2

I want to remove duplicate string also sort the string array in C# I am reading text file which contains data like Blue, Green, Red, Green, Yellow so I am reading data using File.ReadAllLines() which return an array of string now how to sort and remove duplicate.

private string[] sortArray(string[] str)
{
    int cnt = str.Length - 1;
    for (int i = 0; i < cnt; i++)
    {
        for (int j = cnt; j > i; j--)
        {
            if (((IComparable)str[j - 1]).CompareTo(str[j]) > 0)
            {
                var temp = str[j - 1];
                str[j - 1] = str[j];
                str[j] = temp;
            }
        }
    }
    return str;
}

using this above code I can sort the array but how to remove the duplicate string thanks in advance :)

Shams Tech
  • 105
  • 8
ketan patil
  • 65
  • 15
  • 1
    Bubble sort, seriously? Aby reason for not using inbuilt methods? – Ivan Stoev Jun 13 '21 at 10:43
  • Does this answer your question? [Simple bubble sort c#](https://stackoverflow.com/questions/14768010/simple-bubble-sort-c-sharp) – Max Jun 13 '21 at 10:44
  • @Max : this code can also sort but how to remove duplicate from this using this same loop – ketan patil Jun 13 '21 at 10:47
  • @IvanStoev : do you have any simple solution which gives fastest sort and remove duplicate – ketan patil Jun 13 '21 at 10:48
  • As you already able to sort it, check [this](https://stackoverflow.com/questions/9673/how-do-i-remove-duplicates-from-a-c-sharp-array) to remove duplicates – bhaginath Jun 13 '21 at 10:48
  • @Bhaginath : i want remove duplicate also [link](https://stackoverflow.com/a/9677/8505315) – ketan patil Jun 13 '21 at 10:51
  • A simple search found [this article](https://stackoverflow.com/questions/496896/how-to-delete-an-element-from-an-array-in-c-sharp) on how remove an element from array – Max Jun 13 '21 at 10:53
  • Sure. For instance, Instead of `File.ReadAllLines()`, use `File.ReadLines()` + LINQ, e.g. `ReadLines().Disttinc().OrderBy(s => s).ToArray();`. Of course there are many other ways. – Ivan Stoev Jun 13 '21 at 10:56

2 Answers2

3

Here is what you can do

List<string> colorList = new List<string> { "Yellow", "Blue", "Green", "Red", "Blue", "Green", "Red", "Green", "Green", "Yellow" };
colorList = colorList.Distinct().OrderBy(item=> item).ToList();

File.ReadAllLines() will give you string[] and you can apply Dictinct and OrderBy in the same way as mentioned above

Sorting & remove duplicate without using build-in functions

Create a call to methods like this

string[] colorArray = new string[] { "Yellow", "Blue", "Green", "Red", "Blue", "Green", "Red", "Green", "Green", "Yellow" };
colorArray = RemoveDuplicates(colorArray);
Sort(colorArray);

sort using below method

static void Sort(string[] sa)
{
    int pos = 1;
    while (pos < sa.Length)
    {
        if (String.Compare(sa[pos], sa[pos - 1], StringComparison.OrdinalIgnoreCase) >= 0)
        {
            pos++;
        }
        else
        {
            string temp = sa[pos];
            sa[pos] = sa[pos - 1];
            sa[pos - 1] = temp;
            if (pos > 1) pos--;
        }
    }
}

Remove duplicates using this one

static string[] RemoveDuplicates(string[] inputArray)
{

    int length = inputArray.Length;
    for (int i = 0; i < length; i++)
    {
        for (int j = (i + 1); j < length;)
        {
            if (inputArray[i] == inputArray[j])
            {
                for (int k = j; k < length - 1; k++)
                    inputArray[k] = inputArray[k + 1];
                length--;
            }
            else
                j++;
        }
    }

    string[] distinctArray = new string[length];
    for (int i = 0; i < length; i++)
        distinctArray[i] = inputArray[i];

    return distinctArray;

}
1

We can remove duplicate items using below code. In below code instead stack you can use any collection like List, Dictionary and etc. That supports contains function. Using any existing sorting algorithm you can sort the items

string[] colorArray = new string[] { "Yellow", "Blue", "Green", "Red", "Blue", "Green", "Red", "Green", "Green", "Yellow" };
        Stack<string> strStacks = new Stack<string>();
        for (int i = 0; i < colorArray.Length; i++)
        {
            if (strStacks.Contains(colorArray[i]))
            {
                continue;
            }
            else
            {
                strStacks.Push(colorArray[i]);
            }
        }
Rajendar Manda
  • 323
  • 2
  • 10