📌  相关文章
📜  数组中两个奇数发生的元素,所有其他偶数发生的时间

📅  最后修改于: 2021-05-25 10:12:49             🧑  作者: Mango

给定一个数组,其中所有元素除两次外均出现偶数次,请打印出两个奇数出现的元素。可以假设数组的大小至少为2。

例子:

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

Input : arr[] = {15, 10, 10, 50 7, 5, 5, 50, 50, 50, 50, 50}
Output : 7 15

一个简单的解决方案是使用两个嵌套循环。外循环遍历所有元素。内部循环对当前元素的出现进行计数。我们打印出现次数为奇数的元素。如果此解决方案为O(n 2 ),则时间复杂度

更好的解决方案是使用哈希。该解决方案的时间复杂度为O(n),但需要额外的空间。

一个有效的解决方案是使用按位运算运算符。这个想法是基于在两个缺失要素和两个重复要素中使用的方法。

C++
// CPP code to find two odd occurring elements
// in an array where all other elements appear
// even number of times.
#include 
using namespace std;
 
void printOdds(int arr[], int n)
{
    // Find XOR of all numbers
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
 
    // Find a set bit in the XOR (We find
    // rightmost set bit here)
    int set_bit = res & (~(res - 1));
 
    // Traverse through all numbers and
    // divide them in two groups
    // (i) Having set bit set at same
    //     position as the only set bit
    //     in set_bit
    // (ii) Having 0 bit at same position
    //      as the only set bit in set_bit
    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] & set_bit)
            x = x ^ arr[i];
        else
            y = y ^ arr[i];
    }
 
    // XOR of two different sets are our
    // required numbers.
    cout << x << " " << y;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 3, 3, 4, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printOdds(arr, n);
    return 0;
}


Java
// Java code to find two
// odd occurring elements
// in an array where all
// other elements appear
// even number of times.
 
class GFG
{
static void printOdds(int arr[],
                      int n)
{
    // Find XOR of
    // all numbers
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
 
    // Find a set bit in the
    // XOR (We find rightmost
    // set bit here)
    int set_bit = res &
                  (~(res - 1));
 
    // Traverse through all
    // numbers and divide them
    // in two groups (i) Having
    // set bit set at same position
    // as the only set bit in
    // set_bit (ii) Having 0 bit at
    // same position as the only
    // set bit in set_bit
    int x = 0, y = 0;
    for (int i = 0; i < n; i++)
    {
        if ((arr[i] & set_bit) != 0)
            x = x ^ arr[i];
        else
            y = y ^ arr[i];
    }
 
    // XOR of two different
    // sets are our required
    // numbers.
    System.out.println( x + " " + y);
}
 
// Driver code
public static void main(String [] args)
{
    int arr[] = { 2, 3, 3,
                  4, 4, 5 };
    int n = arr.length;
    printOdds(arr, n);
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal


Python3
# Python 3 code to find two
# odd occurring elements in
# an array where all other
# elements appear even number
# of times.
def printOdds(arr, n):
 
    # Find XOR of all numbers
    res = 0
    for i in range(0, n):
        res = res ^ arr[i]
 
    # Find a set bit in
    # the XOR (We find
    # rightmost set bit here)
    set_bit = res & (~(res - 1))
 
    # Traverse through all numbers
    # and divide them in two groups
    # (i) Having set bit set at
    # same position as the only set
    # bit in set_bit
    # (ii) Having 0 bit at same
    # position as the only set
    # bit in set_bit
    x = 0
    y = 0
    for i in range(0, n):
        if (arr[i] & set_bit):
            x = x ^ arr[i]
        else:
            y = y ^ arr[i]
     
    # XOR of two different
    # sets are our
    # required numbers.
    print(x , y, end = "")
 
# Driver code
arr = [2, 3, 3, 4, 4, 5 ]
n = len(arr)
printOdds(arr, n)
 
# This code is contributed
# by Smitha


C#
// C# code to find two
// odd occurring elements
// in an array where all
// other elements appear
// even number of times.
using System;
 
class GFG
{
static void printOdds(int []arr,
                      int n)
{
    // Find XOR of
    // all numbers
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
 
    // Find a set bit in the
    // XOR (We find rightmost
    // set bit here)
    int set_bit = res &
               (~(res - 1));
 
    // Traverse through all
    // numbers and divide them
    // in two groups (i) Having
    // set bit set at same position
    // as the only set bit in
    // set_bit (ii) Having 0 bit at
    // same position as the only
    // set bit in set_bit
    int x = 0, y = 0;
    for (int i = 0; i < n; i++)
    {
        if ((arr[i] & set_bit) != 0)
            x = x ^ arr[i];
        else
            y = y ^ arr[i];
    }
 
    // XOR of two different
    // sets are our required
    // numbers.
    Console.WriteLine(x + " " + y);
}
 
// Driver code
public static void Main()
{
    int []arr = { 2, 3, 3,
                  4, 4, 5 };
    int n = arr.Length;
    printOdds(arr, n);
}
}
 
// This code is contributed by
// Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
5 2

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