给定一个数组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 为 0,并且数字 x 与 0 的 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 基础课程。