Find the Number Occurring Odd Number of Times | GeeksforGeeks

preview_player
Показать описание


This video is contributed by Harshit Jain.
Рекомендации по теме
Комментарии
Автор

Method 3 in JavaScript:
function getOddOccurence(array){
var res = 0;
for (let i=0; i<array.length; i++){
res = res ^ array[i]
}
return res;
}

claytonroberts
Автор

Can we initialise the res value with
res=ar[0];
for(i=1; i<n;
}

// so that we will XOR the first value with the remaining value instead of initialising it with 0.

soumyachoudhary
Автор

please expalin the logic of xor in code also
how xor of 0 and 12 will occurr

Laughter_Dose_On_Youtube
Автор

One confusion: xor is basically modulo operation.
What do you mean by xor all elements. Modulo wrt what?

RelearningSchool
Автор

for hashing solution, i guess C++ map data structure is used. any opinion on this?

spicytuna
Автор

this algo is not working if 2 element having odd occurance i-e {1, 2, 1, 2, 1, 2}

sqammerabbas
Автор

Is there any operation to find even occurence of a number in an array?

ishitagoel
Автор

class Solution{
public:
int getOddOccurrence(int arr[], int n)
{
int res=0;
for(int i=0;i<n;i++)
{
res=res^arr[i];
}
return res;
}

};

wecan
Автор

is this correct in any way??

arr=[1, 2, 3, 1, 3, 2, 3]
arr_set=set(arr)
for i in arr_set:
if arr.count(i)%2!=0:
print(i)

aleenareji
Автор

There is a problem when you give input as {0, 0} even zeros then it prints 0 which is wrong.

BikkiMahato
Автор

can u expalin when more than 1 number are occuring odd no of times

ramalingareddychinta
Автор

in the best solution you have given res = 0 then if 0 occurs odd no of time then also it will be cancelled due to initial 0

jaikumarbohara
Автор

int ar[] = new int[]{2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2, 2};

if add one more 2 at the end of the array it gives answer 7 which is rong

Raj-fdt
Автор

function CounterTimes(arr){
let frequency = {};
let retorno = [];

for(let item of arr){
if(item in frequency){
frequency[item] +=1
}
else{
frequency[item] = 1;
}
}
for(item in frequency){
if(frequency[item] % 2 !== 0){
retorno.push(item);
}
}
return retorno;
}

josebernardo