【问题标题】:Array Duplicate Elimination with c#?用c#进行数组重复消除?
【发布时间】:2013-04-01 03:51:19
【问题描述】:

我这里有一个需要改进的程序。该程序在一个数组中输入 5 个元素,如果有重复则删除。它有效,但问题是它将每个重复项设置为零。我不想显示零。我希望它完全被摧毁和消除。我不希望出现那个重复的元素。这就是我到目前为止所拥有的!可以使用一些帮助。谢谢。

// Gurpreet Singh
// Duplicate Program
using System;

class duplicate 
{
    static void Main() 
    {
    const int Array_Size = 5;
    int [] number = new int [Array_Size];
    int i;

    for ( i = 0; i < Array_Size; i++) 
    {
        Console.Write("Element " + i + ":    ");
        number[i] = Int32.Parse(Console.ReadLine());
        if (number[i] < 9 || number[i] > 101)
        {
            Console.WriteLine("Enter Number between 10 - 100");
            number[i] = Int32.Parse(Console.ReadLine());
        }
    }

    for (i = 0; i < Array_Size; i++)
    {
        for (int j = 0; j < Array_Size; j++) 
        {
            if (i != j)
            {
                if (number[j] == number[i])
                    number[j] = 0;
            }
        }
    }

    Console.WriteLine("Duplicate Removed:");
    for (i = 0; i < Array_Size; i++)
    {
        Console.WriteLine("Element " + i + "    " + number[i]);
    }
    Console.ReadLine();
}
}

【问题讨论】:

    标签: c# arrays for-loop nested duplicate-removal


    【解决方案1】:

    最简单的方法是使用Linq的Distinct方法:

    number = number.Distinct().ToArray();
    

    这将返回一个没有任何重复的新数组。

    【讨论】:

      【解决方案2】:

      重复项显示为零,因为您在该行中将重复项的值分配为零,

      if(number[j]==number[i])
          number[j]=0
      

      要从数组中删除元素,请使用以下代码:

      if(number[j]==number[i])
      {
          int k=j;
          while(k<Array_Size-1)
          {
              number[k]=number[k+1];
              k++;
          }
          Array_Size--;
      }
      

      语句Array_Size--; 已完成,因此最后一个元素不会重复两次

      【讨论】:

        【解决方案3】:

        这是我的完整代码,我在其中添加了一些双循环语句 防止它在数组中插入重复的整数。 看看吧。

         class Program
         {
          static void Main(string[] args)
          {
             const int ARRAY_SIZE = 5;
             int[] ArrayTable = new int[ARRAY_SIZE];
             int Element=0;
             int a;
        
             for(a=0; a<ArrayTable.Length;a++)
             {
        
                Console.Write("Please Enter an integer (between 10-100): ");
                Element = Int32.Parse(Console.ReadLine());
                while (Element < 10 || Element > 100)
                {
                   Console.Write("Try again (between 10-100): ");
                   Element = Int32.Parse(Console.ReadLine());
                }
        
                ArrayTable[a] = Element;
                for (int b = 0; b < a; b++)
                {
                   while (ArrayTable[a] == ArrayTable[b])
                   {
                      Console.Write("Integer Duplicated!\nTry again: ");
                      Element = Int32.Parse(Console.ReadLine());
                      ArrayTable[a] = Element;
                      Console.WriteLine();
                      while (Element < 10 || Element > 100)
                      {
                         Console.Write("Try again (between 10-100): ");
                         Element = Int32.Parse(Console.ReadLine());
                         ArrayTable[a] = Element;
                      }
                   }
                }
             }
        
             for (int c = 0; c < ArrayTable.Length; c++)
             {
                Console.Write("{0} ", ArrayTable[c]);
             }
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-16
          • 1970-01-01
          • 2019-04-08
          • 2019-08-20
          • 2018-10-02
          • 1970-01-01
          • 1970-01-01
          • 2021-01-08
          相关资源
          最近更新 更多