Find the Number Occurring Odd Number of Times | GeeksforGeeks

Sdílet
Vložit
  • čas přidán 31. 03. 2016
  • Explanation for the article: www.geeksforgeeks.org/find-the...
    Read More: www.geeksforgeeks.org/find-th...
    This video is contributed by Harshit Jain.

Komentáře • 31

  • @claytonroberts5881
    @claytonroberts5881 Před 4 lety +1

    Method 3 in JavaScript:
    function getOddOccurence(array){
    var res = 0;
    for (let i=0; i

  • @soumyachoudhary
    @soumyachoudhary Před 3 lety +1

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

  • @RelearningSchool
    @RelearningSchool Před 3 měsíci

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

  • @ishitagoel6556
    @ishitagoel6556 Před 7 lety

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

  • @prashantdhyani8559
    @prashantdhyani8559 Před 3 lety +4

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

  • @spicytuna08
    @spicytuna08 Před 6 lety +1

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

  • @sqammerabbas3811
    @sqammerabbas3811 Před 4 lety +2

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

    • @kalagaarun9638
      @kalagaarun9638 Před 4 lety

      The question expicitly mentions that the number of odd times occurring elements are only 1.

  • @ramalingareddychinta9575
    @ramalingareddychinta9575 Před 6 lety +1

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

  • @BikkiMahato
    @BikkiMahato Před 6 lety

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

    • @Curtisjackson501975
      @Curtisjackson501975 Před 5 lety

      Read the question properly to understand the conditions of values inside the array.

  • @divyachowdhary2574
    @divyachowdhary2574 Před 8 lety +1

    thank u...keep up with gud work...

  • @hermesmercuriustrismegistu4841

    Thx a lot

  • @spicytuna08
    @spicytuna08 Před 6 lety

    awesome algorithm.

  • @jaikumarbohara7964
    @jaikumarbohara7964 Před 4 lety

    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

    • @subhadeepchakraborty681
      @subhadeepchakraborty681 Před 4 lety

      The question reads the array should contain positive integers, so 0 should not be an element in the array.

  • @Bhatonia_Jaat
    @Bhatonia_Jaat Před 3 lety

    ty so much for these videos ;-;

  • @wecan2729
    @wecan2729 Před 3 lety

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

  • @josebernardo4699
    @josebernardo4699 Před 3 lety

    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;
    }

  • @kaifahmad4131
    @kaifahmad4131 Před 2 lety

    XOR the great :)

  • @aleenareji8955
    @aleenareji8955 Před 2 lety

    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)

    • @mohammadammar7485
      @mohammadammar7485 Před 2 lety

      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)

  • @momomiasec1078
    @momomiasec1078 Před 7 lety

    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

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  Před 7 lety +1

      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. :)

    • @momomiasec1078
      @momomiasec1078 Před 7 lety

      thanks i got it; can you explain the logic how its work i dont understand in the video lecture

    • @killersam
      @killersam Před 7 lety

      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

    • @shivshankarsingh3918
      @shivshankarsingh3918 Před 4 lety

      @@GeeksforGeeksVideos True, and if condition on one number occurring odd number of times is removed then hash map will be the best solution.