Yup but you are using an extra space of O(n) and list.count actually takes O(n) time and you are doing it for each element. So your time complexity will be O(n^2)
The problem states that given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Adding another 2 doesn't follow that. After inserting another 2 , there happen to be two numbers, 2 and 5, which appear odd number of times. Hope this solves your confusion. :)
Xor--> 1st(I/P) 2nd(I/P) 0 0 -->0 0 1(A) -->1(A) 1(A) 0 -->1(A) 1 1 -->0 Any input with even occurrence integers will always give 0. E.g--> 1,2,3,1,2,3 In the loop ->0^1-->1 ->1^2->3 ->3^3->0 ->0^1->1 ->1^2->3 ->3^3->0 if u add 1 here it will return 1. Please follow below link stackoverflow.com/a/31673206/4914882
Method 3 in JavaScript:
function getOddOccurence(array){
var res = 0;
for (let i=0; i
Can we initialise the res value with
res=ar[0];
for(i=1; i
One confusion: xor is basically modulo operation.
What do you mean by xor all elements. Modulo wrt what?
Is there any operation to find even occurence of a number in an array?
please expalin the logic of xor in code also
how xor of 0 and 12 will occurr
it will be 12
for hashing solution, i guess C++ map data structure is used. any opinion on this?
yeah we can use unordered_map
this algo is not working if 2 element having odd occurance i-e {1,2,1,2,1,2}
The question expicitly mentions that the number of odd times occurring elements are only 1.
can u expalin when more than 1 number are occuring odd no of times
There is a problem when you give input as {0,0} even zeros then it prints 0 which is wrong.
Read the question properly to understand the conditions of values inside the array.
thank u...keep up with gud work...
You're welcome, Divya!
Thx a lot
awesome algorithm.
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
The question reads the array should contain positive integers, so 0 should not be an element in the array.
ty so much for these videos ;-;
class Solution{
public:
int getOddOccurrence(int arr[], int n)
{
int res=0;
for(int i=0;i
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;
}
XOR the great :)
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)
Yup but you are using an extra space of O(n) and list.count actually takes O(n) time and you are doing it for each element. So your time complexity will be O(n^2)
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
The problem states that given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times.
Adding another 2 doesn't follow that.
After inserting another 2 , there happen to be two numbers, 2 and 5, which appear odd number of times.
Hope this solves your confusion. :)
thanks i got it; can you explain the logic how its work i dont understand in the video lecture
Xor-->
1st(I/P) 2nd(I/P)
0 0 -->0
0 1(A) -->1(A)
1(A) 0 -->1(A)
1 1 -->0
Any input with even occurrence integers will always give 0.
E.g--> 1,2,3,1,2,3
In the loop
->0^1-->1
->1^2->3
->3^3->0
->0^1->1
->1^2->3
->3^3->0
if u add 1 here it will return 1.
Please follow below link
stackoverflow.com/a/31673206/4914882
@@GeeksforGeeksVideos True, and if condition on one number occurring odd number of times is removed then hash map will be the best solution.