Finding Repeat and missing numbers in an array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite
1













Problem Statement:



You are given a read-only array of n integers from 1 to n. Each
integer appears exactly once except A which appears twice and B which
is missing.



Return A and B.



Example



Input:[3 1 2 5 3] 
Output:[3, 4]
A = 3, B = 4



I wrote the following code:



def repeated_number(number_array):

pair_A_B =

n = len(number_array)

for i in number_array:
if(number_array.count(i)==2):
pair_A_B.append(i)
break

sample = range(1,n+1)

diff = list(set(sample)-set(number_array))

pair_A_B.append(diff[0])

return pair_A_B

sample_input = [3,1,2,3,5]

print(repeated_number(sample_input))


The code works fine for this problem on my laptop but when I try to submit it on a coding forum it says my code is not efficient. How can I make it efficient in terms of time?







share|improve this question





















  • I don't have time to write a full answer, but it comes down to this: you have a turned an O(n) problem into an O(n^2) problem. Here is the algorithm I would use: create a list of bools that is the size of number_array. Initialize the elements to false with a list comprehension. Now, iterate through number_array. For each number found, check the value in the bool list at that index - 1. If it's true, add that number to the output list. If it's false, set it to true. Then iterate through the list of bools. Once you find an element that is false, add the index of that element + 1 to the...
    – Mike Borkland
    Jun 24 at 15:51










  • output list. Break from the loop and return the output list.
    – Mike Borkland
    Jun 24 at 15:51






  • 1




    Did you do some research? Various more efficient methods are demonstrated for example at geeksforgeeks.org/find-a-repeating-and-a-missing-number and stackoverflow.com/questions/5766936/…. – And here is another CR question about the same problem codereview.stackexchange.com/questions/194379/…
    – Martin R
    Jun 24 at 16:05







  • 1




    @MartinR my bad, I should have checked.
    – Prathu Baronia
    Jun 24 at 16:16










  • I actually solved by having an empty hashmap and started populating it as I proceeded and had a check on whenever the value goes to 2
    – Prathu Baronia
    Jun 28 at 11:45
















up vote
0
down vote

favorite
1













Problem Statement:



You are given a read-only array of n integers from 1 to n. Each
integer appears exactly once except A which appears twice and B which
is missing.



Return A and B.



Example



Input:[3 1 2 5 3] 
Output:[3, 4]
A = 3, B = 4



I wrote the following code:



def repeated_number(number_array):

pair_A_B =

n = len(number_array)

for i in number_array:
if(number_array.count(i)==2):
pair_A_B.append(i)
break

sample = range(1,n+1)

diff = list(set(sample)-set(number_array))

pair_A_B.append(diff[0])

return pair_A_B

sample_input = [3,1,2,3,5]

print(repeated_number(sample_input))


The code works fine for this problem on my laptop but when I try to submit it on a coding forum it says my code is not efficient. How can I make it efficient in terms of time?







share|improve this question





















  • I don't have time to write a full answer, but it comes down to this: you have a turned an O(n) problem into an O(n^2) problem. Here is the algorithm I would use: create a list of bools that is the size of number_array. Initialize the elements to false with a list comprehension. Now, iterate through number_array. For each number found, check the value in the bool list at that index - 1. If it's true, add that number to the output list. If it's false, set it to true. Then iterate through the list of bools. Once you find an element that is false, add the index of that element + 1 to the...
    – Mike Borkland
    Jun 24 at 15:51










  • output list. Break from the loop and return the output list.
    – Mike Borkland
    Jun 24 at 15:51






  • 1




    Did you do some research? Various more efficient methods are demonstrated for example at geeksforgeeks.org/find-a-repeating-and-a-missing-number and stackoverflow.com/questions/5766936/…. – And here is another CR question about the same problem codereview.stackexchange.com/questions/194379/…
    – Martin R
    Jun 24 at 16:05







  • 1




    @MartinR my bad, I should have checked.
    – Prathu Baronia
    Jun 24 at 16:16










  • I actually solved by having an empty hashmap and started populating it as I proceeded and had a check on whenever the value goes to 2
    – Prathu Baronia
    Jun 28 at 11:45












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1






Problem Statement:



You are given a read-only array of n integers from 1 to n. Each
integer appears exactly once except A which appears twice and B which
is missing.



Return A and B.



Example



Input:[3 1 2 5 3] 
Output:[3, 4]
A = 3, B = 4



I wrote the following code:



def repeated_number(number_array):

pair_A_B =

n = len(number_array)

for i in number_array:
if(number_array.count(i)==2):
pair_A_B.append(i)
break

sample = range(1,n+1)

diff = list(set(sample)-set(number_array))

pair_A_B.append(diff[0])

return pair_A_B

sample_input = [3,1,2,3,5]

print(repeated_number(sample_input))


The code works fine for this problem on my laptop but when I try to submit it on a coding forum it says my code is not efficient. How can I make it efficient in terms of time?







share|improve this question














Problem Statement:



You are given a read-only array of n integers from 1 to n. Each
integer appears exactly once except A which appears twice and B which
is missing.



Return A and B.



Example



Input:[3 1 2 5 3] 
Output:[3, 4]
A = 3, B = 4



I wrote the following code:



def repeated_number(number_array):

pair_A_B =

n = len(number_array)

for i in number_array:
if(number_array.count(i)==2):
pair_A_B.append(i)
break

sample = range(1,n+1)

diff = list(set(sample)-set(number_array))

pair_A_B.append(diff[0])

return pair_A_B

sample_input = [3,1,2,3,5]

print(repeated_number(sample_input))


The code works fine for this problem on my laptop but when I try to submit it on a coding forum it says my code is not efficient. How can I make it efficient in terms of time?









share|improve this question












share|improve this question




share|improve this question








edited Jun 24 at 16:09









Jamal♦

30.1k11114225




30.1k11114225









asked Jun 24 at 14:48









Prathu Baronia

1




1











  • I don't have time to write a full answer, but it comes down to this: you have a turned an O(n) problem into an O(n^2) problem. Here is the algorithm I would use: create a list of bools that is the size of number_array. Initialize the elements to false with a list comprehension. Now, iterate through number_array. For each number found, check the value in the bool list at that index - 1. If it's true, add that number to the output list. If it's false, set it to true. Then iterate through the list of bools. Once you find an element that is false, add the index of that element + 1 to the...
    – Mike Borkland
    Jun 24 at 15:51










  • output list. Break from the loop and return the output list.
    – Mike Borkland
    Jun 24 at 15:51






  • 1




    Did you do some research? Various more efficient methods are demonstrated for example at geeksforgeeks.org/find-a-repeating-and-a-missing-number and stackoverflow.com/questions/5766936/…. – And here is another CR question about the same problem codereview.stackexchange.com/questions/194379/…
    – Martin R
    Jun 24 at 16:05







  • 1




    @MartinR my bad, I should have checked.
    – Prathu Baronia
    Jun 24 at 16:16










  • I actually solved by having an empty hashmap and started populating it as I proceeded and had a check on whenever the value goes to 2
    – Prathu Baronia
    Jun 28 at 11:45
















  • I don't have time to write a full answer, but it comes down to this: you have a turned an O(n) problem into an O(n^2) problem. Here is the algorithm I would use: create a list of bools that is the size of number_array. Initialize the elements to false with a list comprehension. Now, iterate through number_array. For each number found, check the value in the bool list at that index - 1. If it's true, add that number to the output list. If it's false, set it to true. Then iterate through the list of bools. Once you find an element that is false, add the index of that element + 1 to the...
    – Mike Borkland
    Jun 24 at 15:51










  • output list. Break from the loop and return the output list.
    – Mike Borkland
    Jun 24 at 15:51






  • 1




    Did you do some research? Various more efficient methods are demonstrated for example at geeksforgeeks.org/find-a-repeating-and-a-missing-number and stackoverflow.com/questions/5766936/…. – And here is another CR question about the same problem codereview.stackexchange.com/questions/194379/…
    – Martin R
    Jun 24 at 16:05







  • 1




    @MartinR my bad, I should have checked.
    – Prathu Baronia
    Jun 24 at 16:16










  • I actually solved by having an empty hashmap and started populating it as I proceeded and had a check on whenever the value goes to 2
    – Prathu Baronia
    Jun 28 at 11:45















I don't have time to write a full answer, but it comes down to this: you have a turned an O(n) problem into an O(n^2) problem. Here is the algorithm I would use: create a list of bools that is the size of number_array. Initialize the elements to false with a list comprehension. Now, iterate through number_array. For each number found, check the value in the bool list at that index - 1. If it's true, add that number to the output list. If it's false, set it to true. Then iterate through the list of bools. Once you find an element that is false, add the index of that element + 1 to the...
– Mike Borkland
Jun 24 at 15:51




I don't have time to write a full answer, but it comes down to this: you have a turned an O(n) problem into an O(n^2) problem. Here is the algorithm I would use: create a list of bools that is the size of number_array. Initialize the elements to false with a list comprehension. Now, iterate through number_array. For each number found, check the value in the bool list at that index - 1. If it's true, add that number to the output list. If it's false, set it to true. Then iterate through the list of bools. Once you find an element that is false, add the index of that element + 1 to the...
– Mike Borkland
Jun 24 at 15:51












output list. Break from the loop and return the output list.
– Mike Borkland
Jun 24 at 15:51




output list. Break from the loop and return the output list.
– Mike Borkland
Jun 24 at 15:51




1




1




Did you do some research? Various more efficient methods are demonstrated for example at geeksforgeeks.org/find-a-repeating-and-a-missing-number and stackoverflow.com/questions/5766936/…. – And here is another CR question about the same problem codereview.stackexchange.com/questions/194379/…
– Martin R
Jun 24 at 16:05





Did you do some research? Various more efficient methods are demonstrated for example at geeksforgeeks.org/find-a-repeating-and-a-missing-number and stackoverflow.com/questions/5766936/…. – And here is another CR question about the same problem codereview.stackexchange.com/questions/194379/…
– Martin R
Jun 24 at 16:05





1




1




@MartinR my bad, I should have checked.
– Prathu Baronia
Jun 24 at 16:16




@MartinR my bad, I should have checked.
– Prathu Baronia
Jun 24 at 16:16












I actually solved by having an empty hashmap and started populating it as I proceeded and had a check on whenever the value goes to 2
– Prathu Baronia
Jun 28 at 11:45




I actually solved by having an empty hashmap and started populating it as I proceeded and had a check on whenever the value goes to 2
– Prathu Baronia
Jun 28 at 11:45










2 Answers
2






active

oldest

votes

















up vote
1
down vote













In addition to answers given here, you may use collections module for more pythonic code.



import collections

[k for k, v in collections.Counter([1,2,3,3,5]).items() if v > 1]
# [3]


This is O(n).






share|improve this answer




























    up vote
    1
    down vote













    The problem lies in these 2 lines of code:



     for i in number_array:
    if(number_array.count(i)==2):


    Here for each number you are counting the number of occurrences in the array. Now, by doing that, You are reiterating over the numbers which are already counted. The call to number_array.count() searches the entire array, which means even for the last element, it will start from the first and calculate the count. This makes the time complexity $O(n^2)$. But, it can be done in $O(n)$ time.



    Instead of using count, store the numbers in a hash table. And for each new number check, if it already exists in the hash table. If so, that's your duplicate number - A.



    Also, while iterating through the loop, add each number to the other, and get their sum (omitting the double occurrence of A). And since you have a series from 1..n,



    sum(1..n) = n * (n + 1) / 2


    So, the missing number is:



    sum(1..n) - actual sum





    share|improve this answer























      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "196"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197163%2ffinding-repeat-and-missing-numbers-in-an-array%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      In addition to answers given here, you may use collections module for more pythonic code.



      import collections

      [k for k, v in collections.Counter([1,2,3,3,5]).items() if v > 1]
      # [3]


      This is O(n).






      share|improve this answer

























        up vote
        1
        down vote













        In addition to answers given here, you may use collections module for more pythonic code.



        import collections

        [k for k, v in collections.Counter([1,2,3,3,5]).items() if v > 1]
        # [3]


        This is O(n).






        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          In addition to answers given here, you may use collections module for more pythonic code.



          import collections

          [k for k, v in collections.Counter([1,2,3,3,5]).items() if v > 1]
          # [3]


          This is O(n).






          share|improve this answer













          In addition to answers given here, you may use collections module for more pythonic code.



          import collections

          [k for k, v in collections.Counter([1,2,3,3,5]).items() if v > 1]
          # [3]


          This is O(n).







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 28 at 12:21









          sardok

          1113




          1113






















              up vote
              1
              down vote













              The problem lies in these 2 lines of code:



               for i in number_array:
              if(number_array.count(i)==2):


              Here for each number you are counting the number of occurrences in the array. Now, by doing that, You are reiterating over the numbers which are already counted. The call to number_array.count() searches the entire array, which means even for the last element, it will start from the first and calculate the count. This makes the time complexity $O(n^2)$. But, it can be done in $O(n)$ time.



              Instead of using count, store the numbers in a hash table. And for each new number check, if it already exists in the hash table. If so, that's your duplicate number - A.



              Also, while iterating through the loop, add each number to the other, and get their sum (omitting the double occurrence of A). And since you have a series from 1..n,



              sum(1..n) = n * (n + 1) / 2


              So, the missing number is:



              sum(1..n) - actual sum





              share|improve this answer



























                up vote
                1
                down vote













                The problem lies in these 2 lines of code:



                 for i in number_array:
                if(number_array.count(i)==2):


                Here for each number you are counting the number of occurrences in the array. Now, by doing that, You are reiterating over the numbers which are already counted. The call to number_array.count() searches the entire array, which means even for the last element, it will start from the first and calculate the count. This makes the time complexity $O(n^2)$. But, it can be done in $O(n)$ time.



                Instead of using count, store the numbers in a hash table. And for each new number check, if it already exists in the hash table. If so, that's your duplicate number - A.



                Also, while iterating through the loop, add each number to the other, and get their sum (omitting the double occurrence of A). And since you have a series from 1..n,



                sum(1..n) = n * (n + 1) / 2


                So, the missing number is:



                sum(1..n) - actual sum





                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  The problem lies in these 2 lines of code:



                   for i in number_array:
                  if(number_array.count(i)==2):


                  Here for each number you are counting the number of occurrences in the array. Now, by doing that, You are reiterating over the numbers which are already counted. The call to number_array.count() searches the entire array, which means even for the last element, it will start from the first and calculate the count. This makes the time complexity $O(n^2)$. But, it can be done in $O(n)$ time.



                  Instead of using count, store the numbers in a hash table. And for each new number check, if it already exists in the hash table. If so, that's your duplicate number - A.



                  Also, while iterating through the loop, add each number to the other, and get their sum (omitting the double occurrence of A). And since you have a series from 1..n,



                  sum(1..n) = n * (n + 1) / 2


                  So, the missing number is:



                  sum(1..n) - actual sum





                  share|improve this answer















                  The problem lies in these 2 lines of code:



                   for i in number_array:
                  if(number_array.count(i)==2):


                  Here for each number you are counting the number of occurrences in the array. Now, by doing that, You are reiterating over the numbers which are already counted. The call to number_array.count() searches the entire array, which means even for the last element, it will start from the first and calculate the count. This makes the time complexity $O(n^2)$. But, it can be done in $O(n)$ time.



                  Instead of using count, store the numbers in a hash table. And for each new number check, if it already exists in the hash table. If so, that's your duplicate number - A.



                  Also, while iterating through the loop, add each number to the other, and get their sum (omitting the double occurrence of A). And since you have a series from 1..n,



                  sum(1..n) = n * (n + 1) / 2


                  So, the missing number is:



                  sum(1..n) - actual sum






                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Jun 28 at 17:20









                  Sam Onela

                  5,72961543




                  5,72961543











                  answered Jun 28 at 9:51









                  Sayan Bose

                  1111




                  1111






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197163%2ffinding-repeat-and-missing-numbers-in-an-array%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Greedy Best First Search implementation in Rust

                      Function to Return a JSON Like Objects Using VBA Collections and Arrays

                      C++11 CLH Lock Implementation