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

问题描述

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



例如: -



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

推荐答案

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


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


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


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

09-25 02:48