The sword refers to Offer - the repeated numbers in the array

Question Description: In an array of length n all numbers are in the range 0~n-1. Some numbers in the array are repeated, but I don't know how many numbers are repeated, and I don't know how many times each number is repeated. Find any repeated number in the array.

analyze:

Solution 1: Sort first, and then find the duplicate numbers, you can find all the duplicate numbers.

code show as below:

public sealed class SortAndFind
    {
        // quicksort cell sorting
        private static int SortInit(int[] arr, int low, int high)
        {
            int key = arr[low];
            while (low < high)
            {
                while (arr[high] >= key && high > low)
                    high--;
                arr[low] = arr[high];
                while (arr[low] <= key && low < high)
                    low++;
                arr[high] = arr[low];
            }
            arr[low] = key;
            return low;
        }
        //quicksort
        private static void QuickSort(int[] arr, int low, int high)
        {
            if (low >= high)
                return;
            int index = SortInit(arr, low, high);
            QuickSort(arr, low, index - 1);
            QuickSort(arr, index + 1, high);
        }
        // find all duplicate numbers
        public static int[] FindRepeatNums(int[] arr)
        {
            List<int> temp = new List<int>();
            QuickSort(arr, 0, arr.Length - 1);
            foreach (int item in arr)
            {
                Console.Write("{0} ", item);
            }
            Console.WriteLine();
            for (int i = 0; i < arr.Length - 1; i++)
            {
                if (arr[i] != arr[i + 1])
                    continue;
                while (arr[i] == arr[i + 1] && i < arr.Length - 2)
                {
                    i++;
                }
                temp.Add(arr[i]);
            }
            int[] result = temp.ToArray();
            return result;
        }
    }

Solution 2: Using a dictionary to store the value and the number of values, all duplicate numbers can be found.

code show as below:

public sealed class DictionaryAndFind
    {
        public static void Find(int[] arr)
        {
            Dictionary<int, int> dictionary = new Dictionary<int, int>();
            for (int i=0;i<arr.Length;i++)
            {
                if (!dictionary.ContainsKey(arr[i]))
                    dictionary.Add(arr[i], 1);
                else
                    dictionary[arr[i]] += 1;
            }
            int[] keys = dictionary.Keys.ToArray();
            for(int j = 0;j<keys.Length;j++)
            {
                if (dictionary[keys[j]] > 1)
                    Console.Write("{0} ", keys[j]);
            }
        }
    }

Solution 3: Carefully analyze the problem, all numbers are in the range of 0~n-1, grasping this condition, we can think what would happen if there were no repeated numbers? If the order is sorted, the subscript is equal to the value, and the solution is based on this. If the value is equal to the subscript, scan backwards, otherwise, compare with the value whose subscript is the value first, if it is equal, find the duplicate number, otherwise, swap the two numbers. While sorting, find duplicates.

code show as below:

public sealed class ChangeAndFind
    {
        public static int FindOneRepeat(int[] arr)
        {
            if (arr == null || arr.Length <= 0)
                return -1;
            for(int i=0;i<arr.Length;i++)
            {
                if (arr[i] < 0 || arr[i] > arr.Length - 1)
                    return -1;
            }
            int one = 0;
            for(int i=0;i<arr.Length;i++)
            {
                while(arr[i]!=i)
                {
                    int temp = arr[i];
                    if (arr[i] == arr[temp])
                    {
                        one = arr[i];
                        return one;
                    }
                    arr[i] = arr[temp];
                    arr[temp] = temp;
                }
            }
            return one;
        }
    }
Summary: Carefully analyze the problem to find the optimal solution.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326659571&siteId=291194637