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

📅  最后修改于: 2021-09-06 11:27:59             🧑  作者: 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


Javascript


输出:

2

时间复杂度: O(N)
辅助空间: O(N)
相关文章:

  1. 在 O(n) 时间和 O(1) 额外空间中查找重复项 |设置 1
  2. 在 O(n) 中的数组中重复并使用 O(1) 额外空间 |组 2

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live