📜  使用计数排序在值1到N的数组中查找重复项

📅  最后修改于: 2021-05-14 01:15:24             🧑  作者: Mango

给定一个由N个元素组成的常量数组,其中包含从1N – 1的元素,这些数字中的任何一个都出现任意次。

例子:

幼稚的方法:幼稚的方法是首先对给定的数组进行排序,然后查找该数组的相邻位置以查找重复编号。

时间复杂度: O(N * log N)
辅助空间: O(1)

高效方法:为了优化上述方法,其思想是使用计数排序的概念。由于数组中元素的范围是已知的,因此我们可以使用这种排序技术来改善时间复杂度。
这个想法是初始化另一个具有相同大小N的数组(比如count [] ),并将所有元素初始化为0 。然后计算数组中每个元素的出现次数,并更新count []中的计数。打印所有计数大于1的元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to find the duplicate
// number using countig sort method
int findDuplicate(int arr[], int n)
{
    int countArr[n + 1], i;
  
    // Initialize all the elements
    // of the countArr to 0
    for (i = 0; i <= n; i++)
        countArr[i] = 0;
  
    // Count the occurences of each
    // element of the array
    for (i = 0; i <= n; i++)
        countArr[arr[i]]++;
  
    bool a = false;
  
    // Find the element with more
    // than one count
    for (i = 1; i <= n; i++) {
  
        if (countArr[i] > 1) {
            a = true;
            cout << i << ' ';
        }
    }
  
    // If unique elements are ther
    // print "-1"
    if (!a)
        cout << "-1";
}
  
// Driver Code
int main()
{
    // Given N
    int n = 4;
  
    // Given array arr[]
    int arr[] = { 1, 3, 4, 2, 2 };
  
    // Function Call
    findDuplicate(arr, n);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG {
  
    // Function to find the duplicate number
    // using countig sort method
    public static int
    findDuplicate(int arr[], int n)
    {
        int countArr[] = new int[n + 1], i;
  
        // Initialize all the elements of the
        // countArr to 0
        for (i = 0; i <= n; i++)
            countArr[i] = 0;
  
        // Count the occurences of each
        // element of the array
        for (i = 0; i <= n; i++)
            countArr[arr[i]]++;
  
        bool a = false;
  
        // Find the element with more
        // than one count
        for (i = 1; i <= n; i++) {
  
            if (countArr[i] > 1) {
                a = true;
                cout << i << ' ';
            }
        }
        if (!a)
            System.out.println("-1");
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 4;
        int arr[] = { 1, 3, 4, 2, 2 };
  
        // Function Call
        findDuplicate(arr, n);
    }
}


Python3
# Python3 program for the above approach
  
# Function to find the duplicate
# number using countig sort method 
def findDuplicate(arr, n):
  
    # Initialize all the elements
    # of the countArr to 0
    countArr = [0] * (n + 1)
  
    # Count the occurences of each
    # element of the array
    for i in range(n + 1):
        countArr[arr[i]] += 1
  
    a = False
  
    # Find the element with more
    # than one count
    for i in range(1, n + 1):
        if(countArr[i] > 1):
            a = True
            print(i, end = " ")
  
    # If unique elements are there
    # print "-1"
    if(not a):
        print(-1)
  
# Driver code
if __name__ == '__main__':
  
    # Given N
    n = 4
  
    # Given array arr[]
    arr = [ 1, 3, 4, 2, 2 ]
  
    # Function Call
    findDuplicate(arr, n)
  
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to find the duplicate number
// using countig sort method
static void findDuplicate(int []arr, int n)
{
    int []countArr = new int[n + 1];
    int i;
  
    // Initialize all the elements of the
    // countArr to 0
    for(i = 0; i <= n; i++)
       countArr[i] = 0;
  
    // Count the occurences of each
    // element of the array
    for(i = 0; i <= n; i++)
       countArr[arr[i]]++;
  
    bool a = false;
  
    // Find the element with more
    // than one count
    for(i = 1; i <= n; i++) 
    {
       if (countArr[i] > 1)
       {
           a = true;
           Console.Write(i + " ");
        }
    }
    if (!a)
        Console.WriteLine("-1");
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 4;
    int []arr = { 1, 3, 4, 2, 2 };
  
    // Function Call
    findDuplicate(arr, n);
}
}
  
// This code is contributed by Amit Katiyar


输出:
2

时间复杂度: O(N)
辅助空间: O(N)

相关文章:

  1. 查找O(n)时间和O(1)多余空间中的重复项|套装1
  2. 在数组O(n)中复制并通过使用O(1)多余空间|套装2