📌  相关文章
📜  在大小为 n 的排序数组中查找唯一的重复元素

📅  最后修改于: 2021-09-16 11:03:53             🧑  作者: Mango

给定一个包含从 1 到 n-1 范围内的元素的 n 个元素的排序数组,即一个元素出现两次,任务是找到数组中的重复元素。
例子 :

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

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

一种天真的方法是扫描整个数组并检查一个元素是否出现两次,然后返回。这种方法需要 O(n) 时间。
一种有效的方法是使用 Binary Search 。
1- 检查中间元素是否是重复元素。
2-如果不是,则检查中间元素是否处于正确位置,如果是,则开始在右侧搜索重复元素。
3- 否则重复的将在左边。

C++
// C++ program to find the only repeating element in an
// array of size n and elements from range 1 to n-1.
#include 
using namespace std;
 
// Returns index of second appearance of a repeating element
// The function assumes that array elements are in range from
// 1 to n-1.
int findRepeatingElement(int arr[], int low, int high)
{
    // low = 0 , high = n-1;
    if (low > high)
        return -1;
 
    int mid = low + (high - low) / 2;
 
    // Check if the mid element is the repeating one
    if (arr[mid] != mid + 1)
    {
        if (mid > 0 && arr[mid]==arr[mid-1])
            return mid;
 
        // If mid element is not at its position that means
        // the repeated element is in left
        return findRepeatingElement(arr, low, mid-1);
    }
 
    // If mid is at proper position then repeated one is in
    // right.
    return findRepeatingElement(arr, mid+1, high);
}
 
// Driver code
int main()
{
    int arr[] = {1, 2, 3, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int index = findRepeatingElement(arr, 0, n-1);
    if (index != -1)
        cout << arr[index];
    return 0;
}


Java
// Java program to find the only repeating element in an
// array of size n and elements from range 1 to n-1.
 
class Test
{
    // Returns index of second appearance of a repeating element
    // The function assumes that array elements are in range from
    // 1 to n-1.
    static int findRepeatingElement(int arr[], int low, int high)
    {
        // low = 0 , high = n-1;
        if (low > high)
            return -1;
      
        int mid = (low + high) / 2;
      
        // Check if the mid element is the repeating one
        if (arr[mid] != mid + 1)
        {
            if (mid > 0 && arr[mid]==arr[mid-1])
                return mid;
      
            // If mid element is not at its position that means
            // the repeated element is in left
            return  findRepeatingElement(arr, low, mid-1);
        }
      
        // If mid is at proper position then repeated one is in
        // right.
        return findRepeatingElement(arr, mid+1, high);
    }
     
    // Driver method
    public static void main(String[] args)
    {
        int  arr[] = {1, 2, 3, 3, 4, 5};
        int index = findRepeatingElement(arr, 0, arr.length-1);
        if (index != -1)
            System.out.println(arr[index]);
    }
}


Python
# Python program to find the only repeating element in an
# array of size n and elements from range 1 to n-1
 
# Returns index of second appearance of a repeating element
# The function assumes that array elements are in range from
# 1 to n-1.
def findRepeatingElement(arr, low, high):
 
    # low = 0 , high = n-1
    if low > high:
        return -1
 
    mid = (low + high) / 2
 
    # Check if the mid element is the repeating one
    if (arr[mid] != mid + 1):
     
        if (mid > 0 and arr[mid]==arr[mid-1]):
            return mid
 
        # If mid element is not at its position that means
        # the repeated element is in left
        return  findRepeatingElement(arr, low, mid-1)
 
    # If mid is at proper position then repeated one is in
    # right.
    return findRepeatingElement(arr, mid+1, high)
 
# Driver code
arr = [1, 2, 3, 3, 4, 5]
n = len(arr)
index = findRepeatingElement(arr, 0, n-1)
if (index is not -1):
    print arr[index]
 
#This code is contributed by Afzal Ansari


C#
// C# program to find the only repeating
// element in an array of size n and
// elements from range 1 to n-1.
using System;
 
class Test
{
    // Returns index of second appearance of a
    // repeating element. The function assumes that
    // array elements are in range from 1 to n-1.
    static int findRepeatingElement(int []arr, int low,
                                              int high)
    {
        // low = 0 , high = n-1;
        if (low > high)
            return -1;
     
        int mid = (low + high) / 2;
     
        // Check if the mid element
        // is the repeating one
        if (arr[mid] != mid + 1)
        {
            if (mid > 0 && arr[mid]==arr[mid-1])
                return mid;
     
            // If mid element is not at its position
            // that means the repeated element is in left
            return findRepeatingElement(arr, low, mid-1);
        }
     
        // If mid is at proper position
        // then repeated one is in right.
        return findRepeatingElement(arr, mid+1, high);
    }
     
    // Driver method
    public static void Main()
    {
        int []arr = {1, 2, 3, 3, 4, 5};
        int index = findRepeatingElement(arr, 0, arr.Length-1);
        if (index != -1)
        Console.Write(arr[index]);
    }
}
 
// This code is contributed by Nitin Mittal.


PHP
 $high)
        return -1;
 
    $mid = floor(($low + $high) / 2);
 
    // Check if the mid element
    // is the repeating one
    if ($arr[$mid] != $mid + 1)
    {
        if ($mid > 0 && $arr[$mid] ==
                        $arr[$mid - 1])
            return $mid;
 
        // If mid element is not at
        // its position that means
        // the repeated element is in left
        return findRepeatingElement($arr, $low,
                                    $mid - 1);
    }
 
    // If mid is at proper position
    // then repeated one is in right.
    return findRepeatingElement($arr, $mid + 1,
                                        $high);
}
 
// Driver code
$arr = array(1, 2, 3, 3, 4, 5);
$n = sizeof($arr);
$index = findRepeatingElement($arr, 0,
                              $n - 1);
if ($index != -1)
echo $arr[$index];
 
// This code is contributed
// by nitin mittal.
?>


Javascript


输出 :

3

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