不使用 LINQ 查找数组列表中的重复值

发布于 2024-10-09 22:33:30 字数 100 浏览 5 评论 0原文

我有一个数组列表,其中有几个重复的项目。我需要知道每个重复项目的数量。我使用的是2.0所以不能使用linq。

我之前也发过类似的问题,但是我的问题不太清楚。 谢谢 普拉迪

I have an arraylist with few duplicate items. I need to know the count of each duplicated item. I am using 2.0 so cannot use linq.

I had posted a similar question earlier, but my question was not clear.
Thanks
Prady

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

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

发布评论

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

评论(3

风铃鹿 2024-10-16 22:33:30

我过去曾经做过一些事情。我的解决方案是循环遍历 ArrayList 并将计数存储在字典中。然后循环遍历字典以显示结果:

        ArrayList list = new ArrayList();
        list.Add(1);
        list.Add("test");
        list.Add("test");
        list.Add("test");
        list.Add(2);
        list.Add(3);
        list.Add(2);

        Dictionary<Object, int> itemCount = new Dictionary<object, int>();

        foreach (object o in list)
        {
            if (itemCount.ContainsKey(o))
                itemCount[o]++;
            else
                itemCount.Add(o, 1);
        }

        foreach (KeyValuePair<Object, int> item in itemCount)
        {
            if (item.Value > 1)
                Console.WriteLine(item.Key + " count: " + item.Value);
        }

输出:

test count: 3
2 count: 2

编辑
意识到我使用了 var 关键字,这不是 2.0 功能。将其替换为 KeyValuePair。

I've done something in the past. My solution was to loop through the ArrayList and store the counts in a dictionary. Then loop though the dictionary to display the results:

        ArrayList list = new ArrayList();
        list.Add(1);
        list.Add("test");
        list.Add("test");
        list.Add("test");
        list.Add(2);
        list.Add(3);
        list.Add(2);

        Dictionary<Object, int> itemCount = new Dictionary<object, int>();

        foreach (object o in list)
        {
            if (itemCount.ContainsKey(o))
                itemCount[o]++;
            else
                itemCount.Add(o, 1);
        }

        foreach (KeyValuePair<Object, int> item in itemCount)
        {
            if (item.Value > 1)
                Console.WriteLine(item.Key + " count: " + item.Value);
        }

Output:

test count: 3
2 count: 2

Edit
Realized I used the var keyword which is not a 2.0 feature. Replaced it with KeyValuePair.

断舍离 2024-10-16 22:33:30

选项 1:对列表进行排序,然后对相邻的 Equal 项目进行计数(要求您重写类的 Equals 方法)

选项 2:使用您的唯一标识符(但是您将两个对象定义为相等)作为字典的键,并将每个对象添加到该条目中。

Option 1: Sort the list and then count adjacently Equal items (requires you to override the Equals method for your class)

Option 2: Use your unique identifier (however you're defining two objects to be equal) as the key for a Dictionary and add each of your objects to that entry.

很酷不放纵 2024-10-16 22:33:30

很久以前我需要一个类似的项目,并为它做了一个功能

    static Dictionary<object, int> GetDuplicates(ArrayList list, out ArrayList uniqueList)
    {
        uniqueList = new ArrayList();
        Dictionary<object, int> dups = new Dictionary<object, int>();
        foreach (object o in list)
        {
            if (uniqueList.Contains(o))
                if (!dups.ContainsKey(o))
                    dups.Add(o, 2);
                else
                    dups[o]++;
            else
                uniqueList.Add(o);
        }
        return dups;
    }

i needed something similar for a project a long time ago, and made a function for it

    static Dictionary<object, int> GetDuplicates(ArrayList list, out ArrayList uniqueList)
    {
        uniqueList = new ArrayList();
        Dictionary<object, int> dups = new Dictionary<object, int>();
        foreach (object o in list)
        {
            if (uniqueList.Contains(o))
                if (!dups.ContainsKey(o))
                    dups.Add(o, 2);
                else
                    dups[o]++;
            else
                uniqueList.Add(o);
        }
        return dups;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文