如何在 C# 中删除重复的数字 (int)

发布于 2025-01-10 15:15:04 字数 3538 浏览 0 评论 0原文

如何使用 for 循环删除列表框中的重复项。我尝试了几种方法,但它们不起作用。我尝试了最后一种方法,但不是删除重复项,而是将它们替换为 0。

我的作业是在列表框中生成随机数,并在单独的列表框中分隔为偶数和奇数。然后我必须将平均值和选定的数字计算为立方和平方根。一切正常。除了重复的。需要一些帮助。

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AssignmentQ1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {


            List<int> lstRN = new List<int>();

            Random rnd = new Random();
            int numbers = rnd.Next(1, 1000);

            int item = 0;
            int digit = 0;
            int totalOdd, totalEven, countEven, countOdd;
            totalOdd = totalEven = countEven = countOdd = 0;
            double avg = 0.0;
            double sum = 0.0;

           

            for (int i=0; i<numbers; i++)
            {
                item = rnd.Next(1, 300);
                lstRN.Add(item);
                
            }

            lstRN.Sort();


            digit = lstRN[0];

            for (int k= 0; k<lstRN.Count; k++)
            {
                if(lstRN[k] == digit)
                {
                    lstRN[k]=0;

                }
                else
                {
                    digit = lstRN[k];
                }

            }



            foreach (int num in lstRN)
            {
                if(num % 2 == 0)
                {
                    lstEven.Items.Add(num);
                    totalEven += num;
                    
                }
                else
                {
                    lstOdd.Items.Add(num);
                    totalOdd += num;
                   
                }

            }

           




            /**var _items1 = this.lstRN.Items.Cast<string>().Distinct().ToList();
            this.lstRN.Items.Clear();
            foreach (var item1 in _items1)
            {
                this.lstEven.Items.Add(item1);
            }

            var _items2 = this.lstRN.Items.Cast<string>().Distinct().ToList();
            this.lstRN.Items.Clear();
            foreach (var item2 in _items2)
            {
                this.lstOdd.Items.Add(item2);
            }**/


            countEven = lstEven.Items.Count;
            lblCountEven.Text = countEven.ToString();
            avg = item / countEven;
            lblAverageEven.Text = avg.ToString();

            countOdd = lstOdd.Items.Count;
            lblCountOdd.Text = countOdd.ToString();
            avg = item / countOdd;
            lblAverageOdd.Text = avg.ToString();



        }

        private void lstEven_SelectedIndexChanged(object sender, EventArgs e)
        {
            string chosen = lstEven.SelectedItem.ToString();
            int c = int.Parse(chosen);
            double sr = Math.Sqrt(c);
            lblSquare.Text = sr.ToString();

        }

        private void lstOdd_SelectedIndexChanged(object sender, EventArgs e)
        {
            string chosen = lstOdd.SelectedItem.ToString();
            int c = int.Parse(chosen);
            double cube = Math.Pow(c, 3);
            lblCube.Text = cube.ToString();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

How to remove duplicated in a listbox using for loop. I have tried few methods but they are not working. I've tried a last method but instead of removing the duplicates, they are replaced with 0.

My assignment is about generating a random numbers in a listbox and separating to even and odd numbers in a separate listbox. Then i have to calculate the average and selected numbers into cube and sqroot. Everything is working fine. Except the duplicates. Need some help.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AssignmentQ1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {


            List<int> lstRN = new List<int>();

            Random rnd = new Random();
            int numbers = rnd.Next(1, 1000);

            int item = 0;
            int digit = 0;
            int totalOdd, totalEven, countEven, countOdd;
            totalOdd = totalEven = countEven = countOdd = 0;
            double avg = 0.0;
            double sum = 0.0;

           

            for (int i=0; i<numbers; i++)
            {
                item = rnd.Next(1, 300);
                lstRN.Add(item);
                
            }

            lstRN.Sort();


            digit = lstRN[0];

            for (int k= 0; k<lstRN.Count; k++)
            {
                if(lstRN[k] == digit)
                {
                    lstRN[k]=0;

                }
                else
                {
                    digit = lstRN[k];
                }

            }



            foreach (int num in lstRN)
            {
                if(num % 2 == 0)
                {
                    lstEven.Items.Add(num);
                    totalEven += num;
                    
                }
                else
                {
                    lstOdd.Items.Add(num);
                    totalOdd += num;
                   
                }

            }

           




            /**var _items1 = this.lstRN.Items.Cast<string>().Distinct().ToList();
            this.lstRN.Items.Clear();
            foreach (var item1 in _items1)
            {
                this.lstEven.Items.Add(item1);
            }

            var _items2 = this.lstRN.Items.Cast<string>().Distinct().ToList();
            this.lstRN.Items.Clear();
            foreach (var item2 in _items2)
            {
                this.lstOdd.Items.Add(item2);
            }**/


            countEven = lstEven.Items.Count;
            lblCountEven.Text = countEven.ToString();
            avg = item / countEven;
            lblAverageEven.Text = avg.ToString();

            countOdd = lstOdd.Items.Count;
            lblCountOdd.Text = countOdd.ToString();
            avg = item / countOdd;
            lblAverageOdd.Text = avg.ToString();



        }

        private void lstEven_SelectedIndexChanged(object sender, EventArgs e)
        {
            string chosen = lstEven.SelectedItem.ToString();
            int c = int.Parse(chosen);
            double sr = Math.Sqrt(c);
            lblSquare.Text = sr.ToString();

        }

        private void lstOdd_SelectedIndexChanged(object sender, EventArgs e)
        {
            string chosen = lstOdd.SelectedItem.ToString();
            int c = int.Parse(chosen);
            double cube = Math.Pow(c, 3);
            lblCube.Text = cube.ToString();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙沙粒小 2025-01-17 15:15:04

如果您是 LINQ 爱好者,您可以使用以下扩展方法

public static IEnumerable<T> RemoveDuplicates<T>(this IEnumerable<T> source)
{
    return source.GroupBy(x => x).Select(y => y.First());
}

它将收集所有相等的项目并选择其中的第一个

If you are a LINQ addict, you can use the following extension method

public static IEnumerable<T> RemoveDuplicates<T>(this IEnumerable<T> source)
{
    return source.GroupBy(x => x).Select(y => y.First());
}

It will collect all the items that are the equals and select the first of them

二货你真萌 2025-01-17 15:15:04

有专用的工具/类型 - HashSet

var uniqueNumbers = numbers.ToHashSet();

要在集合中收集唯一的随机数,

var randomNumbers = new HashSet<int>();
while (randomNumbers.Count < numbers)
{
    var number = rnd.Next(1, 300);
    randomNumbers.Add(number)
}

.Add 方法仅当值不在集合中时才会添加给定值。
哈希集。添加(T)方法

There are dedicated tool/type for that - HashSet<int>.

var uniqueNumbers = numbers.ToHashSet();

To gather unique random numbers in the collection

var randomNumbers = new HashSet<int>();
while (randomNumbers.Count < numbers)
{
    var number = rnd.Next(1, 300);
    randomNumbers.Add(number)
}

.Add method will add given value only if value is not in the set yet.
HashSet.Add(T) Method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文