📌  相关文章
📜  查找数组中唯一不同的元素

📅  最后修改于: 2022-05-13 01:57:13.193000             🧑  作者: Mango

查找数组中唯一不同的元素

给定一个除一个元素外所有元素都相同的整数数组,找出数组中唯一不同的元素。可以假设阵列的大小至少为二。
例子:

一个简单的解决方案是遍历数组。对于每个元素,检查它是否与其他元素不同。该解决方案的时间复杂度为 O(n*n)
更好的解决方案是使用散列。我们计算所有元素的频率。哈希表将有两个元素。我们打印值(或频率)等于 1 的元素。此解决方案在 O(n) 时间内有效,但需要 O(n) 额外空间。
一个有效的解决方案是首先检查前三个元素。可能有两种情况,

  1. 两个元素是相同的,即一个是根据问题中的给定条件而不同的。在这种情况下,不同的元素在前三个中,所以我们返回不同的元素。
  2. 所有三个元素都是相同的。在这种情况下,不同的元素位于剩余的数组中。所以我们从第四个元素开始遍历数组,并简单地检查当前元素的值是否与前一个不同。

下面是上述想法的实现。



C++
// C++ program to find the only different
// element in an array.
#include 
using namespace std;
 
// Function to find minimum range
// increments to sort an array
int findTheOnlyDifferent(int arr[], int n)
{
    // Array size must be at least two.
    if (n == 1)
        return -1;
 
    // If there are two elements, then we
    // can return any one element as different
    if (n == 2)
        return 0;
 
    // Check if the different element is among
    // first three
    if (arr[0] == arr[1] && arr[0] != arr[2])
        return 2;
    if (arr[0] == arr[2] && arr[0] != arr[1])
        return 1;
    if (arr[1] == arr[2] && arr[0] != arr[1])
        return 0;
 
    // If we reach here, then first three elements
    // must be same (assuming that only one element
    // is different.
    for (int i = 3; i < n; i++)
        if (arr[i] != arr[i - 1])
            return i;
 
    return -1;
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 20, 20, 20, 20 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findTheOnlyDifferent(arr, n);
    return 0;
}


Java
// Java program to find the only different
// element in an array.
 
public class GFG
{
// Function to find minimum range
// increments to sort an array
static int findTheOnlyDifferent(int[] arr, int n)
{
    // Array size must be at least two.
    if (n == 1)
        return -1;
 
    // If there are two elements, then we
    // can return any one element as different
    if (n == 2)
        return 0;
 
    // Check if the different element is among
    // first three
    if (arr[0] == arr[1] && arr[0] != arr[2])
        return 2;
    if (arr[0] == arr[2] && arr[0] != arr[1])
        return 1;
    if (arr[1] == arr[2] && arr[0] != arr[1])
        return 0;
 
    // If we reach here, then first three elements
    // must be same (assuming that only one element
    // is different.
    for (int i = 3; i < n; i++)
        if (arr[i] != arr[i - 1])
            return i;
 
    return -1;
}
 
// Driver Code
public static void main(String args[])
{
    int[] arr = { 10, 20, 20, 20, 20 };
    int n = arr.length;
    System.out.println(findTheOnlyDifferent(arr, n));
}
 
// This code is contributed
// by Ryuga
 
}


Python3
# Python3 program to find the only
# different element in an array.
 
# Function to find minimum range
# increments to sort an array
def findTheOnlyDifferent(arr, n):
 
    # Array size must be at least two.
    if n == 1:
        return -1
 
    # If there are two elements,
    # then we can return any one
    # element as different
    if n == 2:
        return 0
 
    # Check if the different element
    # is among first three
    if arr[0] == arr[1] and arr[0] != arr[2]:
        return 2
    if arr[0] == arr[2] and arr[0] != arr[1]:
        return 1
    if arr[1] == arr[2] and arr[0] != arr[1]:
        return 0
 
    # If we reach here, then first
    # three elements must be same
    # (assuming that only one element
    # is different.
    for i in range(3, n):
        if arr[i] != arr[i - 1]:
            return i
 
    return -1
 
# Driver Code
if __name__ == "__main__":
     
    arr = [10, 20, 20, 20, 20]
    n = len(arr)
    print(findTheOnlyDifferent(arr, n))
 
# This code is contributed
# by Rituraj Jain


C#
// C# program to find the only different
// element in an array.
using System;
 
class GFG
{
// Function to find minimum range
// increments to sort an array
static int findTheOnlyDifferent(int[] arr, int n)
{
    // Array size must be at least two.
    if (n == 1)
        return -1;
 
    // If there are two elements, then we
    // can return any one element as different
    if (n == 2)
        return 0;
 
    // Check if the different element is among
    // first three
    if (arr[0] == arr[1] && arr[0] != arr[2])
        return 2;
    if (arr[0] == arr[2] && arr[0] != arr[1])
        return 1;
    if (arr[1] == arr[2] && arr[0] != arr[1])
        return 0;
 
    // If we reach here, then first three elements
    // must be same (assuming that only one element
    // is different.
    for (int i = 3; i < n; i++)
        if (arr[i] != arr[i - 1])
            return i;
 
    return -1;
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 10, 20, 20, 20, 20 };
    int n = arr.Length;
    Console.Write(findTheOnlyDifferent(arr, n));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP


C++
#include 
using namespace std;
 
void solve()
{
  int arr[] = { 10, 20, 20, 20, 20 };
  int n = sizeof(arr) / sizeof(arr[0]);
  int arr2[n];
  for(int i=0;i


输出
0

时间复杂度: O(n)
辅助空间: O(1)

第二种方法:使用 STL

解决这个问题的另一种方法是使用向量方法。就像我们将简单地复制另一个数组中的所有元素,然后我们将简单地将新排序数组的第一个元素与前一个元素进行比较,而不相同的索引将是我们的答案。

下面是上述方法的实现:

C++

#include 
using namespace std;
 
void solve()
{
  int arr[] = { 10, 20, 20, 20, 20 };
  int n = sizeof(arr) / sizeof(arr[0]);
  int arr2[n];
  for(int i=0;i
输出
0

时间复杂度: O(n)

辅助空间: O(1)