Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
how do i search and delete all the duplicates in a string list array

for example:-

C#
List<string> st = new List<string> { "Hello", "World", "Welcome", "To", "Csharp", "Welcome", "World", "Hello", "To", "Hello" }; //Example string array


now duplicates should be deleted and remaining should be saved in the new list array and also i want the positions of the deleted items in new array

i did this in easy way but not by using list or genric collections

C#
using System;

namespace testing
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int i = 0, j = 0, k = 0, n = 0, ctr = 0;

                Console.WriteLine("Please enter how many elements you want to enter..");
                n = int.Parse(Console.ReadLine());

                string[] a = new string[n];
                
                Console.WriteLine("Enter elements ");
                for (i = 0; i < n; i++)
                {
                    a[i] = Console.ReadLine();
                }

                // Here checking of duplicate elements
                for (i = 0; i < n; i++)
                {
                    for (j = i + 1; j < n; j++)
                    {
                        if (a[i] == a[j])
                        {
                            n = n - 1;
                            for (k = j; k < n; k++)
                            {
                                a[k] = a[k + 1];
                            }
                            ctr = 1;
                            j = j - 1;
                        }
                    }
                }


                if (ctr == 0)
                {
                    Console.WriteLine("Array does not contain duplicate elements");
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("After deletion the duplicate elements are :-");
                    for (i = 0; i < n; i++)
                    {
                        Console.WriteLine(a[i]);
                    }
                }
            }
            
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.Read();
        }
    }
}




please help
Posted
Updated 10-Dec-15 8:41am
v5
Comments
Sergey Alexandrovich Kryukov 10-Dec-15 13:05pm    
Why not blocking adding duplicates in first place?
—SA
Matt T Heffron 10-Dec-15 13:22pm    
The nature of the question and your "please help ASAP" implies that this is likely homework that you've put off until "the last minute".
I'm sure I could come up with a solution in a few minutes, but that wouldn't really help you to learn.
You didn't show us any effort at trying to solve this yourself.
Use "Improve question" and show us what you have tried and where you are still having difficulty and you're likely to get good advice.
Palash Sachan 10-Dec-15 13:34pm    
sir..u r correct..this is my homework..and i have tried this in easy way like this..see the ques again
Matt T Heffron 10-Dec-15 14:41pm    
Your code doesn't actually do what you said you need.
You're using an array of a predetermined size when a List<string> would have been much simpler. (That's what you showed in your original statement of the problem.)
You aren't generating a "new list array" for the result. (a List is not an array. Confusing this is likely to cause you much grief in the future. List and array both implement the IList<T> interface which provides the indexed access ability.)
You never collect or report the index values of the deleted elements.
I usually don't like providing complete solutions to peoples' homework questions, but I've added Solution 3, below.
Remember, submitting any of these solutions as your own work is plagiarism! Learn from them and then submit your own solution.
(None of us will ever know if you submitted our work as yours. But you will know that you cheated!)
Palash Sachan 10-Dec-15 21:56pm    
dear sir..i just made the program what i could make at this moment as i m new to c#..and so i was just asking to how to make it in as the list so that it can be simple..and NO i m not cheating on anyone sir..i m new to it..and i know that if i will do so than it will not be good for me as then i will not learn anything..and thanks for the help and ur solution sir :)

This is similar to Solution 2 by ridoy, except it preserves the original word order generating a new list for duplicate free result.
It is also O(n) in time complexity. (Which isn't a big deal unless st gets long!)
C#
List<string> st = new List<string> { "Hello", "World", "Welcome", "To", "Csharp", "Welcome", "World", "Hello", "To", "Hello" }; //Example string array
HashSet<string> seen = new HashSet<string>();
List<string> duplicateFree = new List<string>();
List<int> deletedIndices = new List<int>();
int ix = 0;
foreach (var s in st)
{
  // seen.Add(s) returns false if s is already in seen
  // so false means this s is a duplicate
  if (seen.Add(s))
  {
    duplicateFree.Add(s);
  }
  else
  {
    deletedIndices.Add(ix);
  }
  ++ix;
}
// duplicateFree is the cleaned-up list:
// { "Hello", "World", "Welcome", "To", "Csharp" }
// If really necessary to be an array:
int[] deletedIndicesArray = deletedIndices.ToArray();
 
Share this answer
 
Comments
ridoy 10-Dec-15 14:42pm    
great man, kudos for extending my answer, a 5 for you,
Matt T Heffron 10-Dec-15 14:48pm    
Thanks!
You can try something like this way:
C#
yourList.Sort();
int index = 0;
List<int> deletedIndex = new List<int>();
while (index < yourList.Count - 1)
{
    if (yourList[index] == yourList[index + 1])
    {
        yourList.RemoveAt(index);
        //index represents the position of deleted items
        deletedIndex.Add(index);
    }
    else
        index++;
}

// You can convert it back to an array if you would like to
int[] deletedIndexArray= deletedIndex.ToArray();
 
Share this answer
 
v4
Comments
Matt T Heffron 10-Dec-15 13:25pm    
Two points:
The question "smells" of being homework, so giving a complete solution is not doing the OP any long term benefit.
OP said the duplicate-free list needed to be a new list. (Presumably, so the deleted index values still correlate to something.)
ridoy 10-Dec-15 13:27pm    
@Matt, answer is already updated, cheers!
Palash Sachan 10-Dec-15 13:47pm    
this is it..this is what i wanted..thanks @ridoy
ridoy 10-Dec-15 14:39pm    
Glad that helps you, i haven't tested the code, even don't write code in C# for long time. I just try to show you a way logically, nice to see that works for you, though someone voted a 1 for it, cheers!
Matt T Heffron 10-Dec-15 14:50pm    
This doesn't preserve the original word order, and it is O(n²).
Still the OP says it is what is wanted, so ...
You can use Distinct to remove duplicate, see following code

C#
List<string> st = new List<string> { "Hello", "World", "Welcome", "To", "Csharp", "Welcome", "World", "Hello", "To", "Hello" }; //Example string array
           List<string> lst = st.Distinct().ToList();
 
Share this answer
 
v2
Comments
Matt T Heffron 10-Dec-15 13:11pm    
This doesn't give a separate array of the positions of the deleted strings, as required.
ridoy 10-Dec-15 13:35pm    
@Sumon, its not what Matt wanted, its what OP wants: "also i want the positions of the deleted items in new array"
Palash Sachan 10-Dec-15 13:44pm    
nice trick..thanks @Anisuzzaman Sumon for the quick answer :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900