判断数组中是否有相同的数

Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it. You are allowed to destroy the array if you like. [ I ended up giving about 4 or 5 different solutions for this, each supposedly better than the others ].

给定一个大小为N的数组,每一个数都是在1N之间,判断里面是不是有重复的数。可以将破坏原数组。
            int[] arr = new int[] { 5, 2, 4, 7, 4, 6, 1 };
            bool result = true;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                for (int j = i+1; j < arr.Length - 1; j++)
                {
                    if (arr[i] == arr[j])
                    {
                        result = false;
                        break;
                    }
                }
                if (result == false)
                {
                    break;
                }
            }
希望高手们更给出更优秀的算法

转载于:https://www.cnblogs.com/KevinPan/archive/2009/09/13/1565885.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值