在 O(n) 中的数组中重复并使用 O(1) 额外空间 |第 2 组
给定一个包含从 0 到 n-1 的元素的 n 个元素的数组,其中任何一个数字出现任意次数,在 O(n) 中找到这些重复数字,并且只使用恒定的内存空间。
例子:
Input: n = 7 , array = {1, 2, 3, 1, 3, 6, 6}
Output: 1, 3 and 6.
Explanation: Duplicate element in the array are 1 , 3 and 6
Input: n = 6, array = {5, 3, 1, 3, 5, 5}
Output: 3 and 5.
Explanation: Duplicate element in the array are 3 and 6
我们在下面的帖子中讨论了解决这个问题的方法:
在 O(n) 中的数组中重复并使用 O(1) 额外空间 |第 2 组。
但是上述方法存在一个问题。它多次打印重复的数字。
我们强烈建议您单击此处并进行练习,然后再继续使用解决方案。
方法:基本思想是使用HashMap来解决问题。但是有一个问题,数组中的数字是从 0 到 n-1,输入数组的长度为 n。因此,输入数组可以用作 HashMap。在遍历数组时,如果遇到元素a则将第a%n '个元素的值增加 n。可以通过将第a%n个元素除以 n 来检索频率。
算法:
- 从头到尾遍历给定的数组。
- 对于数组中的每个元素,将arr[i]%n 'th 元素增加 n。
- 现在再次遍历数组并打印所有那些arr[i]/n大于 1 的索引 i。这保证了数字n已添加到该索引。
注意:这种方法有效,因为所有元素都在 0 到 n-1 的范围内,并且 arr[i]/n 只有在值“i”出现多次时才会大于 1。
下面是上述方法的实现:
CPP
// C++ program to print all elements that
// appear more than once.
#include
using namespace std;
// function to find repeating elements
void printRepeating(int arr[], int n)
{
// First check all the values that are
// present in an array then go to that
// values as indexes and increment by
// the size of array
for (int i = 0; i < n; i++)
{
int index = arr[i] % n;
arr[index] += n;
}
// Now check which value exists more
// than once by dividing with the size
// of array
for (int i = 0; i < n; i++)
{
if ((arr[i] / n) >= 2)
cout << i << " ";
}
}
// Driver code
int main()
{
int arr[] = { 1, 6, 3, 1, 3, 6, 6 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
cout << "The repeating elements are: \n";
// Function call
printRepeating(arr, arr_size);
return 0;
}
Java
// Java program to print all elements that
// appear more than once.
import java.util.*;
class GFG {
// function to find repeating elements
static void printRepeating(int arr[], int n)
{
// First check all the values that are
// present in an array then go to that
// values as indexes and increment by
// the size of array
for (int i = 0; i < n; i++)
{
int index = arr[i] % n;
arr[index] += n;
}
// Now check which value exists more
// than once by dividing with the size
// of array
for (int i = 0; i < n; i++)
{
if ((arr[i] / n) >= 2)
System.out.print(i + " ");
}
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 6, 3, 1, 3, 6, 6 };
int arr_size = arr.length;
System.out.println("The repeating elements are: ");
// Function call
printRepeating(arr, arr_size);
}
}
Python3
# Python3 program to
# print all elements that
# appear more than once.
# function to find
# repeating elements
def printRepeating(arr, n):
# First check all the
# values that are
# present in an array
# then go to that
# values as indexes
# and increment by
# the size of array
for i in range(0, n):
index = arr[i] % n
arr[index] += n
# Now check which value
# exists more
# than once by dividing
# with the size
# of array
for i in range(0, n):
if (arr[i]/n) >= 2:
print(i, end=" ")
# Driver code
arr = [1, 6, 3, 1, 3, 6, 6]
arr_size = len(arr)
print("The repeating elements are:")
# Function call
printRepeating(arr, arr_size)
# This code is contributed
# by Shreyanshi Arun.
C#
// C# program to print all elements that
// appear more than once.
using System;
class GFG {
// function to find repeating elements
static void printRepeating(int[] arr, int n)
{
// First check all the values that are
// present in an array then go to that
// values as indexes and increment by
// the size of array
for (int i = 0; i < n; i++)
{
int index = arr[i] % n;
arr[index] += n;
}
// Now check which value exists more
// than once by dividing with the size
// of array
for (int i = 0; i < n; i++)
{
if ((arr[i] / n) >= 2)
Console.Write(i + " ");
}
}
// Driver code
public static void Main()
{
int[] arr = { 1, 6, 3, 1, 3, 6, 6 };
int arr_size = arr.Length;
Console.Write("The repeating elements are: "
+ "\n");
// Function call
printRepeating(arr, arr_size);
}
}
PHP
= 2)
echo $i , " ";
}
}
// Driver code
$arr = array(1, 6, 3, 1, 3, 6, 6);
$arr_size = sizeof($arr) /
sizeof($arr[0]);
echo "The repeating elements are: \n";
// Function call
printRepeating( $arr, $arr_size);
// This code is contributed by nitin mittal.
?>
Javascript
输出
The repeating elements are:
1 3 6
复杂性分析:
- 时间复杂度: O(n)。
只需要两次遍历。所以时间复杂度是O(n) - 辅助空间: O(1)。
由于不需要额外的空间,所以空间复杂度是恒定的