如何搜索并删除字符串列表数组中的所有重复项 [英] How Do I Search And Delete All The Duplicates In A String List Array

查看:53
本文介绍了如何搜索并删除字符串列表数组中的所有重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何搜索和删除字符串列表数组中的所有重复项



例如: -



how do i search and delete all the duplicates in a string list array

for example:-

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





现在应该删除重复项,剩下的应该保存在新的列表数组中,我也希望新数组中已删除项目的位置



i这样做很简单,但没有使用列表或genric集合





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

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

推荐答案

这与ridoy的解决方案2类似,不同之处在于它保留原始单词顺序,为重复的免费结果生成新列表。 />
时间复杂度也 O(n)。 (除非 st 获得,否则这不是什么大问题!)

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!)
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();


你可以尝试这样的方式:

You can try something like this way:
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();


您可以使用Distinct删除重复项,请参阅以下代码



You can use Distinct to remove duplicate, see following code

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();


这篇关于如何搜索并删除字符串列表数组中的所有重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆