📌  相关文章
📜  在O(n)中并通过使用O(1)多余空间在数组中查找重复项

📅  最后修改于: 2021-04-24 18:58:49             🧑  作者: Mango

给定一个包含n + 1个整数的数组arr [],其中每个整数在1到n之间(包括1和n)。只有一个重复元素,在O(n)时间复杂度和O(1)空间中找到重复元素。
例子 :

Input  : arr[] = {1, 4, 3, 4, 2} 
Output : 4

Input  : arr[] = {1, 3, 2, 1}
Output : 1

方法 :
首先,这个问题的约束意味着必须存在一个循环。因为数组arr []中的每个数字都在1到n之间,所以它必然指向存在的索引。因此,可以无限遍历该列表,这意味着存在一个循环。另外,因为0不能作为数组arr []中的值出现,所以arr [0]不能成为循环的一部分。因此,以这种方式从arr [0]遍历数组等效于遍历循环链表。就像链表循环一样,可以解决该问题。
下面是上述方法的实现:

C++
// CPP code to find the repeated elements
// in the array where every other is present once
#include 
using namespace std;
 
// Function to find duplicate
int findDuplicate(int arr[])
{
    // Find the intersection point of
    // the slow and fast.
    int slow = arr[0];
    int fast = arr[0];
    do
    {
        slow = arr[slow];
        fast = arr[arr[fast]];
    } while (slow != fast);
 
    // Find the "entrance" to the cycle.
    int ptr1 = arr[0];
    int ptr2 = slow;
    while (ptr1 != ptr2)
    {
        ptr1 = arr[ptr1];
        ptr2 = arr[ptr2];
    }
 
    return ptr1;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 3, 2, 1 };
     
    cout << findDuplicate(arr) << endl;
     
    return 0;
}


Java
// Java code to find the repeated
// elements in the array where
// every other is present once
import java.util.*;
 
class GFG
{
 
// Function to find duplicate
public static int findDuplicate(int []arr)
{
    // Find the intersection
    // point of the slow and fast.
    int slow = arr[0];
    int fast = arr[0];
    do
    {
        slow = arr[slow];
        fast = arr[arr[fast]];
    } while (slow != fast);
 
    // Find the "entrance"
    // to the cycle.
    int ptr1 = arr[0];
    int ptr2 = slow;
    while (ptr1 != ptr2)
    {
        ptr1 = arr[ptr1];
        ptr2 = arr[ptr2];
    }
 
    return ptr1;
}
 
// Driver Code
public static void main(String[] args)
{
    int []arr = {1, 3, 2, 1};
 
    System.out.println("" +
               findDuplicate(arr));
 
    System.exit(0);
}
}
 
// This code is contributed
// by Harshit Saini


Python3
# Python code to find the
# repeated elements in the
# array where every other
# is present once
 
# Function to find duplicate
def findDuplicate(arr):
 
    # Find the intersection
    # point of the slow and fast.
    slow = arr[0]
    fast = arr[0]
    while True:
        slow = arr[slow]
        fast = arr[arr[fast]]
        if slow == fast:
            break
 
    # Find the "entrance"
    # to the cycle.
    ptr1 = arr[0]
    ptr2 = slow
    while ptr1 != ptr2:
        ptr1 = arr[ptr1]
        ptr2 = arr[ptr2]
         
    return ptr1
     
# Driver code
if __name__ == '__main__':
     
     
    arr = [ 1, 3, 2, 1 ]
 
    print(findDuplicate(arr))
 
 
# This code is contributed
# by Harshit Saini


C#
// C# code to find the repeated
// elements in the array where
// every other is present once
using System;
 
class GFG
{
 
// Function to find duplicate
public static int findDuplicate(int []arr)
{
    // Find the intersection
    // point of the slow and fast.
    int slow = arr[0];
    int fast = arr[0];
    do
    {
        slow = arr[slow];
        fast = arr[arr[fast]];
    } while (slow != fast);
 
    // Find the "entrance"
    // to the cycle.
    int ptr1 = arr[0];
    int ptr2 = slow;
    while (ptr1 != ptr2)
    {
        ptr1 = arr[ptr1];
        ptr2 = arr[ptr2];
    }
 
    return ptr1;
}
 
// Driver Code
public static void Main()
{
    int[] arr = {1, 3, 2, 1};
 
    Console.WriteLine("" +
            findDuplicate(arr));
 
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
1