📌  相关文章
📜  C / C ++程序查找出现的奇数次

📅  最后修改于: 2021-05-31 22:16:55             🧑  作者: Mango

给定一个数组arr [],它由偶数次出现的正整数组成,但一个数为奇数次。任务是找到这个奇数的出现次数。

例子 :

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

Input : arr = {5, 7, 2, 7, 5, 2, 5}
Output : 5

天真的方法:一个简单的解决方案是运行两个嵌套循环。外循环一个接一个地选取所有元素,内循环计算外循环选取的元素出现的次数。

C++
// C++ program to find the element
// occurring odd number of times
#include 
using namespace std;
 
// Function to find the element
// occurring odd number of times
int getOddOccurrence(int arr[], int arr_size)
{
    for (int i = 0; i < arr_size; i++) {
 
        int count = 0;
 
        for (int j = 0; j < arr_size; j++) {
            if (arr[i] == arr[j])
                count++;
        }
        if (count % 2 != 0)
            return arr[i];
    }
    return -1;
}
 
// driver code
int main()
{
    int arr[] = { 2, 3, 5, 4, 5, 2,
                  4, 3, 5, 2, 4, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function calling
    cout << getOddOccurrence(arr, n);
 
    return 0;
}


C
// C program to find the element
// occurring odd number of times
#include 
 
// Function to find the element
// occurring odd number of times
int getOddOccurrence(int arr[], int arr_size)
{
    for(int i = 0; i < arr_size; i++)
    {
        int count = 0;
 
        for(int j = 0; j < arr_size; j++)
        {
            if (arr[i] == arr[j])
                count++;
        }
        if (count % 2 != 0)
            return arr[i];
    }
    return -1;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 3, 5, 4, 5, 2,
                  4, 3, 5, 2, 4, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function calling
    printf("%d",getOddOccurrence(arr, n));
     
    return 0;
}
 
// This code is contributed by rbbansal


C++
// C++ program to find the element
// occurring odd number of times
#include 
using namespace std;
 
// Function to find element occurring
// odd number of times
int getOddOccurrence(int ar[], int ar_size)
{
    int res = 0;
    for (int i = 0; i < ar_size; i++)
        res = res ^ ar[i];
 
    return res;
}
 
/* Driver function to test above function */
int main()
{
    int ar[] = { 2, 3, 5, 4, 5,
                 2, 4, 3, 5, 2,
                 4, 4, 2 };
    int n = sizeof(ar) / sizeof(ar[0]);
 
    // Function calling
    cout << getOddOccurrence(ar, n);
 
    return 0;
}


C
// C program to find the element
// occurring odd number of times
#include 
 
// Function to find element occurring
// odd number of times
int getOddOccurrence(int ar[], int ar_size)
{
    int res = 0;
    for (int i = 0; i < ar_size; i++)
        res = res ^ ar[i];
 
    return res;
}
 
/* Driver function to test above function */
int main()
{
    int ar[] = { 2, 3, 5, 4, 5,
                 2, 4, 3, 5, 2,
                 4, 4, 2 };
    int n = sizeof(ar) / sizeof(ar[0]);
 
    // Function calling
    printf("%d", getOddOccurrence(ar, n));
    return 0;
}


输出:
5

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

更好的方法:更好的解决方案是使用哈希。使用数组元素作为键,并将其计数作为值。创建一个空的哈希表。一对一遍历给定的数组元素并存储计数。该解决方案的时间复杂度为O(n)。但是它需要额外的空间来进行哈希处理。
时间复杂度: O(N)
辅助空间: O(N)

高效方法:最佳解决方案是对所有元素进行按位XOR。所有元素的XOR给我们奇数出现的元素。请注意,如果两个元素相同,则两个元素的XOR为0,且x与0的数字x的XOR为x。

下面是上述方法的实现。

C++

// C++ program to find the element
// occurring odd number of times
#include 
using namespace std;
 
// Function to find element occurring
// odd number of times
int getOddOccurrence(int ar[], int ar_size)
{
    int res = 0;
    for (int i = 0; i < ar_size; i++)
        res = res ^ ar[i];
 
    return res;
}
 
/* Driver function to test above function */
int main()
{
    int ar[] = { 2, 3, 5, 4, 5,
                 2, 4, 3, 5, 2,
                 4, 4, 2 };
    int n = sizeof(ar) / sizeof(ar[0]);
 
    // Function calling
    cout << getOddOccurrence(ar, n);
 
    return 0;
}

C

// C program to find the element
// occurring odd number of times
#include 
 
// Function to find element occurring
// odd number of times
int getOddOccurrence(int ar[], int ar_size)
{
    int res = 0;
    for (int i = 0; i < ar_size; i++)
        res = res ^ ar[i];
 
    return res;
}
 
/* Driver function to test above function */
int main()
{
    int ar[] = { 2, 3, 5, 4, 5,
                 2, 4, 3, 5, 2,
                 4, 4, 2 };
    int n = sizeof(ar) / sizeof(ar[0]);
 
    // Function calling
    printf("%d", getOddOccurrence(ar, n));
    return 0;
}
输出:
5

时间复杂度: O(N)
辅助空间: O(1)
有关更多详细信息,请参阅完整的文章查找出现次数奇数次数!

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。