📌  相关文章
📜  数组中精确重复 'k' 次的最小元素。

📅  最后修改于: 2021-10-27 07:10:56             🧑  作者: Mango

给定一个大小为 n 的数组,目标是找出恰好重复 ‘k’ 次的最小数字,其中 k > 0?
假设数组只有正整数,并且对于每个 i = 0 到 n -1,1 <= arr[i] < 1000。
例子:

Input : arr[] = {2 2 1 3 1}
        k = 2
Output: 1
Explanation:
Here in array,
2 is repeated 2 times
1 is repeated 2 times 
3 is repeated 1 time
Hence 2 and 1 both are repeated 'k' times
i.e 2 and min(2, 1) is 1

Input : arr[] = {3 5 3 2}
        k = 1
Output : 2
Explanation:
Both 2 and 5 are repeating 1 time but
min(5, 2) is 2

简单的方法:一个简单的方法是使用两个嵌套循环。外循环从最左边的元素开始一个一个地选取一个元素。内部循环检查它的右侧是否存在相同的元素。如果存在,增加计数并使我们在右侧得到的数字为负,以防止它再次计数。

C++
// C++ program to find smallest number
// in array that is repeated exactly
// 'k' times.
#include 
using namespace std;
 
const int MAX = 1000;
 
int findDuplicate(int arr[], int n, int k)
{
    // Since arr[] has numbers in range from
    // 1 to MAX
    int res = MAX + 1;
    for (int i = 0; i < n; i++) {
        if (arr[i] > 0) {
 
            // set count to 1 as number is present
            // once
            int count = 1;
            for (int j = i + 1; j < n; j++)
                if (arr[i] == arr[j])
                    count += 1;
 
            // If frequency of number is equal to 'k'
            if (count == k)
                res = min(res, arr[i]);
        }
    }
    return res;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 2, 1, 3, 1 };
    int k = 2;
    int n = sizeof(arr) / (sizeof(arr[0]));
    cout << findDuplicate(arr, n, k);
    return 0;
}


Java
// Java program to find smallest number
// in array that is repeated exactly
// 'k' times.
public class GFG {
    static final int MAX = 1000;
 
    // finds the smallest number in arr[]
    // that is repeated k times
    static int findDuplicate(int arr[], int n, int k)
    {
        // Since arr[] has numbers in range from
        // 1 to MAX
        int res = MAX + 1;
        for (int i = 0; i < n; i++) {
            if (arr[i] > 0) {
 
                // set count to 1 as number is
                // present once
                int count = 1;
                for (int j = i + 1; j < n; j++)
                    if (arr[i] == arr[j])
                        count += 1;
 
                // If frequency of number is equal
                // to 'k'
                if (count == k)
                    res = Math.min(res, arr[i]);
            }
        }
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 2, 1, 3, 1 };
        int k = 2;
        int n = arr.length;
        System.out.println(findDuplicate(arr, n, k));
    }
}
// This article is contributed by Sumit Ghosh


Python3
# Python 3 program to find smallest
# number in array that is repeated
# exactly 'k' times.
MAX = 1000
 
def findDuplicate(arr, n, k):
 
    # Since arr[] has numbers in
    # range from 1 to MAX
    res = MAX + 1
     
    for i in range(0, n):
        if (arr[i] > 0):
 
            # set count to 1 as number
            # is present once
            count = 1
            for j in range(i + 1, n):
                if (arr[i] == arr[j]):
                    count += 1
 
            # If frequency of number is equal to 'k'
            if (count == k):
                res = min(res, arr[i])
     
    return res
 
# Driver code
arr = [2, 2, 1, 3, 1]
k = 2
n = len(arr)
print(findDuplicate(arr, n, k))
 
# This code is contributed by Smitha Dinesh Semwal.


C#
// C# program to find smallest number
// in array that is repeated exactly
// 'k' times.
using System;
 
public class GFG {
     
    static int MAX = 1000;
 
    // finds the smallest number in arr[]
    // that is repeated k times
    static int findDuplicate(int[] arr,
                              int n, int k)
    {
         
        // Since arr[] has numbers in range
        // from 1 to MAX
        int res = MAX + 1;
         
        for (int i = 0; i < n; i++)
        {
            if (arr[i] > 0)
            {
 
                // set count to 1 as number
                // is present once
                int count = 1;
                for (int j = i + 1; j < n; j++)
                    if (arr[i] == arr[j])
                        count += 1;
 
                // If frequency of number is
                // equal to 'k'
                if (count == k)
                    res = Math.Min(res, arr[i]);
            }
        }
         
        return res;
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 2, 2, 1, 3, 1 };
        int k = 2;
        int n = arr.Length;
         
        Console.WriteLine(
                      findDuplicate(arr, n, k));
    }
}
 
// This article is contributed by vt_m.


PHP
 0)
        {
 
            // set count to 1 as number is
            // present once
            $count = 1;
            for ($j = $i + 1; $j < $n; $j++)
                if ($arr[$i] == $arr[$j])
                    $count += 1;
 
            // If frequency of number is
            // equal to 'k'
            if ($count == $k)
                $res = min($res, $arr[$i]);
        }
    }
    return $res;
}
 
// Driver code
$arr = array(2, 2, 1, 3, 1);
$k = 2;
$n = count($arr);
echo findDuplicate($arr, $n, $k);
 
// This code is contributed by Rajput-Ji
?>


Javascript


C++
// C++ program to find smallest number
// in array that is repeated exactly
// 'k' times.
#include 
using namespace std;
 
int findDuplicate(int arr[], int n, int k)
{
    // Sort the array
    sort(arr, arr + n);
 
    // Find the first element with exactly
    // k occurrences.
    int i = 0;
    while (i < n) {
        int j, count = 1;
        for (j = i + 1; j < n && arr[j] == arr[i]; j++)
            count++;
 
        if (count == k)
            return arr[i];
 
        i = j;
    }
 
    return -1;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 2, 1, 3, 1 };
    int k = 2;
    int n = sizeof(arr) / (sizeof(arr[0]));
    cout << findDuplicate(arr, n, k);
    return 0;
}


Java
// Java program to find smallest number
// in array that is repeated exactly
// 'k' times.
import java.util.Arrays;
public class GFG {
 
    // finds the smallest number in arr[]
    // that is repeated k times
    static int findDuplicate(int arr[], int n, int k)
    {
        // Sort the array
        Arrays.sort(arr);
 
        // Find the first element with exactly
        // k occurrences.
        int i = 0;
        while (i < n) {
            int j, count = 1;
            for (j = i + 1; j < n && arr[j] == arr[i]; j++)
                count++;
 
            if (count == k)
                return arr[i];
 
            i = j;
        }
 
        return -1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 2, 1, 3, 1 };
        int k = 2;
        int n = arr.length;
        System.out.println(findDuplicate(arr, n, k));
    }
}
// This article is contributed by Sumit Ghosh


Python
# Python program to find smallest number
# in array that is repeated exactly
# 'k' times.
 
def findDuplicate(arr, n, k):
     
    # Sort the array
    arr.sort()
  
    # Find the first element with exactly
    # k occurrences.
    i = 0
    while (i < n):
        j, count = i + 1, 1
        while (j < n and arr[j] == arr[i]):
            count += 1
            j += 1
  
        if (count == k):
            return arr[i]
  
        i = j
         
    return -1
  
# Driver code
arr = [ 2, 2, 1, 3, 1 ];
k = 2
n = len(arr)
print findDuplicate(arr, n, k)
 
# This code is contributed by Sachin Bisht


C#
// C# program to find smallest number
// in array that is repeated exactly
// 'k' times.
using System;
 
public class GFG {
 
    // finds the smallest number in arr[]
    // that is repeated k times
    static int findDuplicate(int[] arr,
                             int n, int k)
    {
         
        // Sort the array
        Array.Sort(arr);
 
        // Find the first element with
        // exactly k occurrences.
        int i = 0;
        while (i < n) {
            int j, count = 1;
            for (j = i + 1; j < n &&
                     arr[j] == arr[i]; j++)
                count++;
 
            if (count == k)
                return arr[i];
 
            i = j;
        }
 
        return -1;
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 2, 2, 1, 3, 1 };
        int k = 2;
        int n = arr.Length;
         
        Console.WriteLine(
               findDuplicate(arr, n, k));
    }
}
 
// This article is contributed by vt_m.


PHP


Javascript


C++
// C++ program to find smallest number
// in array that is repeated exactly
// 'k' times.
#include 
using namespace std;
 
const int MAX = 1000;
 
int findDuplicate(int arr[], int n, int k)
{
    // Computing frequencies of all elements
    int freq[MAX];
    memset(freq, 0, sizeof(freq));
    for (int i = 0; i < n; i++) {
        if (arr[i] < 1 && arr[i] > MAX) {
            cout << "Out of range";
            return -1;
        }
        freq[arr[i]] += 1;
    }
 
    // Finding the smallest element with
    // frequency as k
    for (int i = 0; i < MAX; i++) {
 
        // If frequency of any of the number
        // is equal to k starting from 0
        // then return the number
        if (freq[i] == k)
            return i;
    }
 
    return -1;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 2, 1, 3, 1 };
    int k = 2;
    int n = sizeof(arr) / (sizeof(arr[0]));
    cout << findDuplicate(arr, n, k);
    return 0;
}


Java
// Java program to find smallest number
// in array that is repeated exactly
// 'k' times.
public class GFG {
 
    static final int MAX = 1000;
 
    // finds the smallest number in arr[]
    // that is repeated k times
    static int findDuplicate(int arr[], int n, int k)
    {
        // Computing frequencies of all elements
        int[] freq = new int[MAX];
 
        for (int i = 0; i < n; i++) {
            if (arr[i] < 1 && arr[i] > MAX) {
                System.out.println("Out of range");
                return -1;
            }
            freq[arr[i]] += 1;
        }
 
        // Finding the smallest element with
        // frequency as k
        for (int i = 0; i < MAX; i++) {
 
            // If frequency of any of the number
            // is equal to k starting from 0
            // then return the number
            if (freq[i] == k)
                return i;
        }
 
        return -1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 2, 1, 3, 1 };
        int k = 2;
        int n = arr.length;
        System.out.println(findDuplicate(arr, n, k));
    }
}
// This article is contributed by Sumit Ghosh


Python
# Python program to find smallest number
# in array that is repeated exactly
# 'k' times.
 
MAX = 1000
  
def findDuplicate(arr, n, k):
     
    # Computing frequencies of all elements
    freq = [0 for i in range(MAX)]
     
    for i in range(n):
        if (arr[i] < 1 and arr[i] > MAX):
            print "Out of range"
            return -1
        freq[arr[i]] += 1
  
    # Finding the smallest element with
    # frequency as k
    for i in range(MAX):
     
        # If frequency of any of the number
        # is equal to k starting from 0
        # then return the number
        if (freq[i] == k):
            return i
     
    return -1
     
# Driver code
arr = [ 2, 2, 1, 3, 1 ]
k = 2
n = len(arr)
print findDuplicate(arr, n, k)
 
# This code is contributed by Sachin Bisht


C#
// C# program to find smallest number
// in array that is repeated exactly
// 'k' times.
using System;
 
public class GFG {
 
    static int MAX = 1000;
 
    // finds the smallest number in arr[]
    // that is repeated k times
    static int findDuplicate(int[] arr,
                            int n, int k)
    {
         
        // Computing frequencies of all
        // elements
        int[] freq = new int[MAX];
 
        for (int i = 0; i < n; i++)
        {
            if (arr[i] < 1 && arr[i] > MAX)
            {
                Console.WriteLine("Out of range");
                return -1;
            }
             
            freq[arr[i]] += 1;
        }
 
        // Finding the smallest element with
        // frequency as k
        for (int i = 0; i < MAX; i++) {
 
            // If frequency of any of the
            // number is equal to k starting
            // from 0 then return the number
            if (freq[i] == k)
                return i;
        }
 
        return -1;
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 2, 2, 1, 3, 1 };
        int k = 2;
        int n = arr.Length;
         
        Console.WriteLine(
               findDuplicate(arr, n, k));
    }
}
 
// This article is contributed by vt_m.


PHP
 $MAX)
        {
            echo "Out of range";
            return -1;
        }
        $freq[$arr[$i]] += 1;
    }
 
    // Finding the smallest element with
    // frequency as k
    for ($i = 0; $i < $MAX; $i++)
    {
 
        // If frequency of any of the number
        // is equal to k starting from 0
        // then return the number
        if ($freq[$i] == $k)
            return $i;
    }
 
    return -1;
}
 
    // Driver code
    $arr = array( 2, 2, 1, 3, 1 );
    $k = 2;
    $n = count($arr);
    echo findDuplicate($arr, $n, $k);
 
// This code is contributed by mits
?>


Javascript


输出:

1

时间复杂度: O(n 2 )
辅助空间: O(1)
此解决方案不需要数组元素在有限范围内。
更好的解决方案:对输入数组进行排序并找到第一个具有恰好 k 次出现次数的元素。

C++

// C++ program to find smallest number
// in array that is repeated exactly
// 'k' times.
#include 
using namespace std;
 
int findDuplicate(int arr[], int n, int k)
{
    // Sort the array
    sort(arr, arr + n);
 
    // Find the first element with exactly
    // k occurrences.
    int i = 0;
    while (i < n) {
        int j, count = 1;
        for (j = i + 1; j < n && arr[j] == arr[i]; j++)
            count++;
 
        if (count == k)
            return arr[i];
 
        i = j;
    }
 
    return -1;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 2, 1, 3, 1 };
    int k = 2;
    int n = sizeof(arr) / (sizeof(arr[0]));
    cout << findDuplicate(arr, n, k);
    return 0;
}

Java

// Java program to find smallest number
// in array that is repeated exactly
// 'k' times.
import java.util.Arrays;
public class GFG {
 
    // finds the smallest number in arr[]
    // that is repeated k times
    static int findDuplicate(int arr[], int n, int k)
    {
        // Sort the array
        Arrays.sort(arr);
 
        // Find the first element with exactly
        // k occurrences.
        int i = 0;
        while (i < n) {
            int j, count = 1;
            for (j = i + 1; j < n && arr[j] == arr[i]; j++)
                count++;
 
            if (count == k)
                return arr[i];
 
            i = j;
        }
 
        return -1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 2, 1, 3, 1 };
        int k = 2;
        int n = arr.length;
        System.out.println(findDuplicate(arr, n, k));
    }
}
// This article is contributed by Sumit Ghosh

Python

# Python program to find smallest number
# in array that is repeated exactly
# 'k' times.
 
def findDuplicate(arr, n, k):
     
    # Sort the array
    arr.sort()
  
    # Find the first element with exactly
    # k occurrences.
    i = 0
    while (i < n):
        j, count = i + 1, 1
        while (j < n and arr[j] == arr[i]):
            count += 1
            j += 1
  
        if (count == k):
            return arr[i]
  
        i = j
         
    return -1
  
# Driver code
arr = [ 2, 2, 1, 3, 1 ];
k = 2
n = len(arr)
print findDuplicate(arr, n, k)
 
# This code is contributed by Sachin Bisht

C#

// C# program to find smallest number
// in array that is repeated exactly
// 'k' times.
using System;
 
public class GFG {
 
    // finds the smallest number in arr[]
    // that is repeated k times
    static int findDuplicate(int[] arr,
                             int n, int k)
    {
         
        // Sort the array
        Array.Sort(arr);
 
        // Find the first element with
        // exactly k occurrences.
        int i = 0;
        while (i < n) {
            int j, count = 1;
            for (j = i + 1; j < n &&
                     arr[j] == arr[i]; j++)
                count++;
 
            if (count == k)
                return arr[i];
 
            i = j;
        }
 
        return -1;
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 2, 2, 1, 3, 1 };
        int k = 2;
        int n = arr.Length;
         
        Console.WriteLine(
               findDuplicate(arr, n, k));
    }
}
 
// This article is contributed by vt_m.

PHP


Javascript


输出:

1

时间复杂度: O(n Log n)
辅助空间: O(1)
高效的方法:高效的方法基于这样一个事实,即数组的数字范围很小(1 到 1000)。我们通过使用大小为 max 的频率数组并存储该数组中每个数字的频率来解决这个问题。

C++

// C++ program to find smallest number
// in array that is repeated exactly
// 'k' times.
#include 
using namespace std;
 
const int MAX = 1000;
 
int findDuplicate(int arr[], int n, int k)
{
    // Computing frequencies of all elements
    int freq[MAX];
    memset(freq, 0, sizeof(freq));
    for (int i = 0; i < n; i++) {
        if (arr[i] < 1 && arr[i] > MAX) {
            cout << "Out of range";
            return -1;
        }
        freq[arr[i]] += 1;
    }
 
    // Finding the smallest element with
    // frequency as k
    for (int i = 0; i < MAX; i++) {
 
        // If frequency of any of the number
        // is equal to k starting from 0
        // then return the number
        if (freq[i] == k)
            return i;
    }
 
    return -1;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 2, 1, 3, 1 };
    int k = 2;
    int n = sizeof(arr) / (sizeof(arr[0]));
    cout << findDuplicate(arr, n, k);
    return 0;
}

Java

// Java program to find smallest number
// in array that is repeated exactly
// 'k' times.
public class GFG {
 
    static final int MAX = 1000;
 
    // finds the smallest number in arr[]
    // that is repeated k times
    static int findDuplicate(int arr[], int n, int k)
    {
        // Computing frequencies of all elements
        int[] freq = new int[MAX];
 
        for (int i = 0; i < n; i++) {
            if (arr[i] < 1 && arr[i] > MAX) {
                System.out.println("Out of range");
                return -1;
            }
            freq[arr[i]] += 1;
        }
 
        // Finding the smallest element with
        // frequency as k
        for (int i = 0; i < MAX; i++) {
 
            // If frequency of any of the number
            // is equal to k starting from 0
            // then return the number
            if (freq[i] == k)
                return i;
        }
 
        return -1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 2, 1, 3, 1 };
        int k = 2;
        int n = arr.length;
        System.out.println(findDuplicate(arr, n, k));
    }
}
// This article is contributed by Sumit Ghosh

Python

# Python program to find smallest number
# in array that is repeated exactly
# 'k' times.
 
MAX = 1000
  
def findDuplicate(arr, n, k):
     
    # Computing frequencies of all elements
    freq = [0 for i in range(MAX)]
     
    for i in range(n):
        if (arr[i] < 1 and arr[i] > MAX):
            print "Out of range"
            return -1
        freq[arr[i]] += 1
  
    # Finding the smallest element with
    # frequency as k
    for i in range(MAX):
     
        # If frequency of any of the number
        # is equal to k starting from 0
        # then return the number
        if (freq[i] == k):
            return i
     
    return -1
     
# Driver code
arr = [ 2, 2, 1, 3, 1 ]
k = 2
n = len(arr)
print findDuplicate(arr, n, k)
 
# This code is contributed by Sachin Bisht

C#

// C# program to find smallest number
// in array that is repeated exactly
// 'k' times.
using System;
 
public class GFG {
 
    static int MAX = 1000;
 
    // finds the smallest number in arr[]
    // that is repeated k times
    static int findDuplicate(int[] arr,
                            int n, int k)
    {
         
        // Computing frequencies of all
        // elements
        int[] freq = new int[MAX];
 
        for (int i = 0; i < n; i++)
        {
            if (arr[i] < 1 && arr[i] > MAX)
            {
                Console.WriteLine("Out of range");
                return -1;
            }
             
            freq[arr[i]] += 1;
        }
 
        // Finding the smallest element with
        // frequency as k
        for (int i = 0; i < MAX; i++) {
 
            // If frequency of any of the
            // number is equal to k starting
            // from 0 then return the number
            if (freq[i] == k)
                return i;
        }
 
        return -1;
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 2, 2, 1, 3, 1 };
        int k = 2;
        int n = arr.Length;
         
        Console.WriteLine(
               findDuplicate(arr, n, k));
    }
}
 
// This article is contributed by vt_m.

PHP

 $MAX)
        {
            echo "Out of range";
            return -1;
        }
        $freq[$arr[$i]] += 1;
    }
 
    // Finding the smallest element with
    // frequency as k
    for ($i = 0; $i < $MAX; $i++)
    {
 
        // If frequency of any of the number
        // is equal to k starting from 0
        // then return the number
        if ($freq[$i] == $k)
            return $i;
    }
 
    return -1;
}
 
    // Driver code
    $arr = array( 2, 2, 1, 3, 1 );
    $k = 2;
    $n = count($arr);
    echo findDuplicate($arr, $n, $k);
 
// This code is contributed by mits
?>

Javascript


输出:

1

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程