How do I check if my array has repeated values inside it?

前端 未结 8 1341
悲哀的现实
悲哀的现实 2020-12-01 20:55

So here is my array.

double[] testArray = new double[10];
// will generate a random numbers from 1-20, too lazy to write the code

I want to

相关标签:
8条回答
  • 2020-12-01 21:10

    Generic extension method :

    public static bool HasDuplicate<T>(this IEnumerable<T> source, IEqualityComparer<T> comparer)
    {
        if (source == null)
            throw new ArgumentException(nameof(source));
    
        HashSet<T> set = new HashSet<T>(comparer);
        foreach (var item in source)
            if (!set.Add(item))
                return true;
    
        return false;
    }
    
    0 讨论(0)
  • 2020-12-01 21:12

    use a hashset to add members to, then check if there a previous occurrence of current member

    public bool ContainsDuplicate(double[] nums) 
    {
                int size = nums.Length;
                HashSet<double> set1 = new HashSet<double>();
    
                for (int i = 0; i < size; i++)
                {
                    if (set1.Contains(nums[i]))
                    {
                        return true;
                    }
                    else
                    {
                        set1.Add(nums[i]);
                    }
                }
                return false;
    }
    
    0 讨论(0)
  • 2020-12-01 21:17

    Use this:

    bool CheckUniqueness(double[] values)
    {
        var uniqueValues = new HashSet<double>();
        foreach (double d in values)
        {
            if(uniqueValues.Contains(d))
            {
                return false;
            }
            uniqueValues.Add(d);
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-12-01 21:17

    We must initialize j from i on the first loop and add one(i+1) because we want to compare first loop value with the next value of same array.

    int[] arr = new int[]{1,2,3,1,4,2,5,4};
    
    //create one loop for arr values
    for (int i = 0;  i < arr.Length; i++)
    {
        //create nested loop for compare current values with actual value of arr
        for (int j = i+1; j < arr.Length; j++)
        {
    
            //and here we put our condition
            if (arr[i] == arr[j])
            {
                Console.WriteLine(arr[i]);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 21:19

    With (OP) 10 random doubles quite fast. The chance of a repeat: ~0.000002 %.

    static bool repeat(double[] a)
    {
        return
            a[0] == a[1] || a[0] == a[2] || a[0] == a[3] || a[0] == a[4] ||
            a[0] == a[5] || a[0] == a[6] || a[0] == a[7] || a[0] == a[8] ||
            a[0] == a[9] || a[1] == a[2] || a[1] == a[3] || a[1] == a[4] ||
            a[1] == a[5] || a[1] == a[6] || a[1] == a[7] || a[1] == a[8] ||
            a[1] == a[9] || a[2] == a[3] || a[2] == a[4] || a[2] == a[5] ||
            a[2] == a[6] || a[2] == a[7] || a[2] == a[8] || a[2] == a[9] ||
            a[3] == a[4] || a[3] == a[5] || a[3] == a[6] || a[3] == a[7] ||
            a[3] == a[8] || a[3] == a[9] || a[4] == a[5] || a[4] == a[6] ||
            a[4] == a[7] || a[4] == a[8] || a[4] == a[9] || a[5] == a[6] ||
            a[5] == a[7] || a[5] == a[8] || a[5] == a[9] || a[6] == a[7] ||
            a[6] == a[8] || a[6] == a[9] || a[7] == a[8] || a[7] == a[9] ||
            a[8] == a[9];
    }
    

    More general, with 10 numbers ~2 times slower than above,
    but ~7 times faster than the hashset approach.

    static bool repeat(double[] a)
    {
        int k = a.Length - 1;
        if (k < 70)
        {
            double aj;
            for (int i = 0, j; i < k; )
            {
                for (aj = a[k--], j = k; j >= i; j--)
                    if (aj == a[j]) return true;
                for (aj = a[i++], j = i; j <= k; j++)
                    if (aj == a[j]) return true;
            }
            return false;
        }
        var h = new HashSet<double>();
        while (k >= 0) if (!h.Add(a[k--])) return false;
        return true;
    }
    

    Two lines (slow with a repeat ;)

    static bool repeat(double[] a)
    { return (new HashSet<double>(a).Count < a.Length); }
    
    0 讨论(0)
  • 2020-12-01 21:25

    take look at my implementation its generic and efficient

    public static bool HasDuplicates<T>(IList<T> items)
        {
            Dictionary<T, bool> map = new Dictionary<T, bool>();
            for (int i = 0; i < items.Count; i++)
            {
                if (map.ContainsKey(items[i]))
                {
                    return true; // has duplicates
                }
                map.Add(items[i], true);
            }
            return false; // no duplicates
        }
    

    here are some calls

    string[] strings = new[] { "1", "2", "3" };
    Utility.HasDuplicates(strings)// this will return false
    
    int[] items=new []{1,2,3,1};
    Utility.HasDuplicates(items)// this will return true
    
    0 讨论(0)
提交回复
热议问题