Find out n numbers of missing elements from an array in java












11














I have an array which carry some integer numbers. Say,numbers={3,0,1} or say, numbers={9,6,4,2,3,5,7,0,1}. Now I have to find out the missing numbers from the array. As per this example there is only one missing number in each set. 1st one miss 2 and the 2nd one miss 8.



I have already code it. My code not only find out one missing number from specified set it can also find out more than 1 missing numbers from a given set.



But if two consecutive numbers are missing from same set it fail to find out.



My code
import java.util.Arrays;

public class Missing_number
{
public static void main( String args )
{
int numbers={9,6,4,5,7,0,1};
Arrays.sort(numbers);
int i=1;

while ( i < numbers.length )
{
if ( numbers[i] - numbers[i-1] == 1 )
{
}
else
{
System.out.println( "Missing number is " + ( numbers[i-1] + 1 ) );
}
i++;
}
}
}


I am thinking like that if I am able to append the 1st missing number in the array and then start searching then how's the code look like? numbers={9,6,4,5,7,0,1} Now, 8 is already missing from this set. Now I have terminated two more element (2,3) from the list. Output: as per my code: 2,8 But 3 is also missing but that does not display.



I am thinking like if I am able to append 2 in number array then it may be little bit easier. But as we all know Java array is immutable so we cannot increase it's length.



So, maybe I will use List. But in list this type of indexing number[0]=somethingnot supported. So how could I proceed then. Am I using list or still stuck into array?



So I take a attempt to create it with an arraylist.



Mycode(modified version from array)

public class T1 {
public static void main(String args){
List<Integer> numbers=new ArrayList<>();
numbers.add(9);
numbers.add(6);
numbers.add(4);
numbers.add(5);
numbers.add(7);
numbers.add(0);
numbers.add(1);
Collections.sort(numbers);
int i=1;
while(i< numbers.size()) {
if (numbers.get(i) - numbers.get(i-1) == 1) {

} else {
System.out.println("Missing number is " + (numbers.get(i-1) + 1));
numbers.add((numbers.get(i-1)+1));
Collections.sort(numbers);
}
i++;
}

}
}


Arraylist can solve my problem. But is there any possibility that a simple array can solve this problem?










share|improve this question
























  • But ArrayList supports indexing, right?
    – Kingsley
    Dec 17 at 22:04






  • 1




    With a list, instead of subscripting, you use the get(int) method, with the index as the method argument.
    – Ted Hopp
    Dec 17 at 22:07












  • "But as we all know Java array is immutable" - no, Java arrays are mutable. They're not resizable, but they're mutable.
    – user2357112
    Dec 18 at 1:03
















11














I have an array which carry some integer numbers. Say,numbers={3,0,1} or say, numbers={9,6,4,2,3,5,7,0,1}. Now I have to find out the missing numbers from the array. As per this example there is only one missing number in each set. 1st one miss 2 and the 2nd one miss 8.



I have already code it. My code not only find out one missing number from specified set it can also find out more than 1 missing numbers from a given set.



But if two consecutive numbers are missing from same set it fail to find out.



My code
import java.util.Arrays;

public class Missing_number
{
public static void main( String args )
{
int numbers={9,6,4,5,7,0,1};
Arrays.sort(numbers);
int i=1;

while ( i < numbers.length )
{
if ( numbers[i] - numbers[i-1] == 1 )
{
}
else
{
System.out.println( "Missing number is " + ( numbers[i-1] + 1 ) );
}
i++;
}
}
}


I am thinking like that if I am able to append the 1st missing number in the array and then start searching then how's the code look like? numbers={9,6,4,5,7,0,1} Now, 8 is already missing from this set. Now I have terminated two more element (2,3) from the list. Output: as per my code: 2,8 But 3 is also missing but that does not display.



I am thinking like if I am able to append 2 in number array then it may be little bit easier. But as we all know Java array is immutable so we cannot increase it's length.



So, maybe I will use List. But in list this type of indexing number[0]=somethingnot supported. So how could I proceed then. Am I using list or still stuck into array?



So I take a attempt to create it with an arraylist.



Mycode(modified version from array)

public class T1 {
public static void main(String args){
List<Integer> numbers=new ArrayList<>();
numbers.add(9);
numbers.add(6);
numbers.add(4);
numbers.add(5);
numbers.add(7);
numbers.add(0);
numbers.add(1);
Collections.sort(numbers);
int i=1;
while(i< numbers.size()) {
if (numbers.get(i) - numbers.get(i-1) == 1) {

} else {
System.out.println("Missing number is " + (numbers.get(i-1) + 1));
numbers.add((numbers.get(i-1)+1));
Collections.sort(numbers);
}
i++;
}

}
}


Arraylist can solve my problem. But is there any possibility that a simple array can solve this problem?










share|improve this question
























  • But ArrayList supports indexing, right?
    – Kingsley
    Dec 17 at 22:04






  • 1




    With a list, instead of subscripting, you use the get(int) method, with the index as the method argument.
    – Ted Hopp
    Dec 17 at 22:07












  • "But as we all know Java array is immutable" - no, Java arrays are mutable. They're not resizable, but they're mutable.
    – user2357112
    Dec 18 at 1:03














11












11








11


2





I have an array which carry some integer numbers. Say,numbers={3,0,1} or say, numbers={9,6,4,2,3,5,7,0,1}. Now I have to find out the missing numbers from the array. As per this example there is only one missing number in each set. 1st one miss 2 and the 2nd one miss 8.



I have already code it. My code not only find out one missing number from specified set it can also find out more than 1 missing numbers from a given set.



But if two consecutive numbers are missing from same set it fail to find out.



My code
import java.util.Arrays;

public class Missing_number
{
public static void main( String args )
{
int numbers={9,6,4,5,7,0,1};
Arrays.sort(numbers);
int i=1;

while ( i < numbers.length )
{
if ( numbers[i] - numbers[i-1] == 1 )
{
}
else
{
System.out.println( "Missing number is " + ( numbers[i-1] + 1 ) );
}
i++;
}
}
}


I am thinking like that if I am able to append the 1st missing number in the array and then start searching then how's the code look like? numbers={9,6,4,5,7,0,1} Now, 8 is already missing from this set. Now I have terminated two more element (2,3) from the list. Output: as per my code: 2,8 But 3 is also missing but that does not display.



I am thinking like if I am able to append 2 in number array then it may be little bit easier. But as we all know Java array is immutable so we cannot increase it's length.



So, maybe I will use List. But in list this type of indexing number[0]=somethingnot supported. So how could I proceed then. Am I using list or still stuck into array?



So I take a attempt to create it with an arraylist.



Mycode(modified version from array)

public class T1 {
public static void main(String args){
List<Integer> numbers=new ArrayList<>();
numbers.add(9);
numbers.add(6);
numbers.add(4);
numbers.add(5);
numbers.add(7);
numbers.add(0);
numbers.add(1);
Collections.sort(numbers);
int i=1;
while(i< numbers.size()) {
if (numbers.get(i) - numbers.get(i-1) == 1) {

} else {
System.out.println("Missing number is " + (numbers.get(i-1) + 1));
numbers.add((numbers.get(i-1)+1));
Collections.sort(numbers);
}
i++;
}

}
}


Arraylist can solve my problem. But is there any possibility that a simple array can solve this problem?










share|improve this question















I have an array which carry some integer numbers. Say,numbers={3,0,1} or say, numbers={9,6,4,2,3,5,7,0,1}. Now I have to find out the missing numbers from the array. As per this example there is only one missing number in each set. 1st one miss 2 and the 2nd one miss 8.



I have already code it. My code not only find out one missing number from specified set it can also find out more than 1 missing numbers from a given set.



But if two consecutive numbers are missing from same set it fail to find out.



My code
import java.util.Arrays;

public class Missing_number
{
public static void main( String args )
{
int numbers={9,6,4,5,7,0,1};
Arrays.sort(numbers);
int i=1;

while ( i < numbers.length )
{
if ( numbers[i] - numbers[i-1] == 1 )
{
}
else
{
System.out.println( "Missing number is " + ( numbers[i-1] + 1 ) );
}
i++;
}
}
}


I am thinking like that if I am able to append the 1st missing number in the array and then start searching then how's the code look like? numbers={9,6,4,5,7,0,1} Now, 8 is already missing from this set. Now I have terminated two more element (2,3) from the list. Output: as per my code: 2,8 But 3 is also missing but that does not display.



I am thinking like if I am able to append 2 in number array then it may be little bit easier. But as we all know Java array is immutable so we cannot increase it's length.



So, maybe I will use List. But in list this type of indexing number[0]=somethingnot supported. So how could I proceed then. Am I using list or still stuck into array?



So I take a attempt to create it with an arraylist.



Mycode(modified version from array)

public class T1 {
public static void main(String args){
List<Integer> numbers=new ArrayList<>();
numbers.add(9);
numbers.add(6);
numbers.add(4);
numbers.add(5);
numbers.add(7);
numbers.add(0);
numbers.add(1);
Collections.sort(numbers);
int i=1;
while(i< numbers.size()) {
if (numbers.get(i) - numbers.get(i-1) == 1) {

} else {
System.out.println("Missing number is " + (numbers.get(i-1) + 1));
numbers.add((numbers.get(i-1)+1));
Collections.sort(numbers);
}
i++;
}

}
}


Arraylist can solve my problem. But is there any possibility that a simple array can solve this problem?







java arrays algorithm arraylist






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 18 at 1:53









Mis94

7251720




7251720










asked Dec 17 at 22:01









Encipher

18611




18611












  • But ArrayList supports indexing, right?
    – Kingsley
    Dec 17 at 22:04






  • 1




    With a list, instead of subscripting, you use the get(int) method, with the index as the method argument.
    – Ted Hopp
    Dec 17 at 22:07












  • "But as we all know Java array is immutable" - no, Java arrays are mutable. They're not resizable, but they're mutable.
    – user2357112
    Dec 18 at 1:03


















  • But ArrayList supports indexing, right?
    – Kingsley
    Dec 17 at 22:04






  • 1




    With a list, instead of subscripting, you use the get(int) method, with the index as the method argument.
    – Ted Hopp
    Dec 17 at 22:07












  • "But as we all know Java array is immutable" - no, Java arrays are mutable. They're not resizable, but they're mutable.
    – user2357112
    Dec 18 at 1:03
















But ArrayList supports indexing, right?
– Kingsley
Dec 17 at 22:04




But ArrayList supports indexing, right?
– Kingsley
Dec 17 at 22:04




1




1




With a list, instead of subscripting, you use the get(int) method, with the index as the method argument.
– Ted Hopp
Dec 17 at 22:07






With a list, instead of subscripting, you use the get(int) method, with the index as the method argument.
– Ted Hopp
Dec 17 at 22:07














"But as we all know Java array is immutable" - no, Java arrays are mutable. They're not resizable, but they're mutable.
– user2357112
Dec 18 at 1:03




"But as we all know Java array is immutable" - no, Java arrays are mutable. They're not resizable, but they're mutable.
– user2357112
Dec 18 at 1:03












4 Answers
4






active

oldest

votes


















6














This code uses a HashSet:



public static void main(String args) {
int numbers = {9, 6, 4, 5, 7, 0, 1};
Arrays.sort(numbers);
HashSet<Integer> set = new HashSet<>();

for (int i = numbers[0]; i < numbers[numbers.length - 1]; i++) {
set.add(i);
}

for (int i = 0; i < numbers.length; i++) {
set.remove(numbers[i]);
}

for (int x : set) {
System.out.print(x + " ");
}
}


will print:



2 3 8 



Here is how it works:

1. Adds all numbers from the minimum number of the array to the maximum number of the array to the set.

2. Iterates through the array and removes every item of the array from the set.

3. Prints the remaining items in the set, which are all the missing items of the array.






share|improve this answer





























    4














    replace the else clause to be:



    for(int j=numbers[i-1] + 1; j <= numbers[i] - 1; j++) {
    System.out.println( "Missing number is " + ( j ) );
    }


    let's examine the case: {9 ,6 ,4 ,5 ,7 ,0 , 1}
    after sorting it will be: {0, 1, 4, 5, 6, 7, 9}
    now if i is at index 2 it finds the difference between numbers[i] and numbers[i-1] to be not equal 1 (4 - 1 = 3), now you need ALL the numbers between 1 and 4 which are 2, 3 and thus you must loop from numbers[i-1] to numbers[i] (exclusive) in order to achieve this.



    The complexity of this code is big O of N (O(N)), where N is the biggest element in your array.






    share|improve this answer































      1














      There are a lot of questions which are unanswered here. For Example, Does an array always start with zero?, What is the maximum possible size? etc.



      Here is a simple way to approach this problem,




      • Find the maximum number in your set.

      • Create an empty boolean array of the length as that of the max number you found in the last step plus one.

      • Scan your original set and set the value of your new boolean array at the index equal to the number in your original set as true.

      • Finally scan your boolean array to find and pront all the indices with value false.


      Example:



      Original Set: {1,0,3}





      • Step 1: Maximum number in the set = 3


      • Step 2: Boolean Array with Length = 3+1 --> {false, false, false, false}


      • Step 3: While scanning original set and setting values in the boolean array this will be the final state --> {true, true, false, true}


      • Step 4: You'll finally scan the boolean array to print 2 since this it is only index which has value = false






      share|improve this answer





















      • My code shown that it is not mandatory to start an array from zero. As the elements depends upon array index. if(numbers[0]=0 ) then its ok if (numbers[0]=5) then it is also ok. All it is matter that the array is a sorted array.
        – Encipher
        Dec 17 at 22:13



















      0














      int numbers = { 11, 6, 4, 5, 7, 1 };
      Arrays.sort(numbers);
      int numbersArrayIndex = 0;
      for (int i = 0; i < numbers[numbers.length - 1]; i++) {
      if (i == numbers[numbersArrayIndex]) {
      numbersArrayIndex++;
      }
      else {
      System.out.println(i);
      }
      }





      share|improve this answer





















        Your Answer






        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: "1"
        };
        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',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53823608%2ffind-out-n-numbers-of-missing-elements-from-an-array-in-java%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        6














        This code uses a HashSet:



        public static void main(String args) {
        int numbers = {9, 6, 4, 5, 7, 0, 1};
        Arrays.sort(numbers);
        HashSet<Integer> set = new HashSet<>();

        for (int i = numbers[0]; i < numbers[numbers.length - 1]; i++) {
        set.add(i);
        }

        for (int i = 0; i < numbers.length; i++) {
        set.remove(numbers[i]);
        }

        for (int x : set) {
        System.out.print(x + " ");
        }
        }


        will print:



        2 3 8 



        Here is how it works:

        1. Adds all numbers from the minimum number of the array to the maximum number of the array to the set.

        2. Iterates through the array and removes every item of the array from the set.

        3. Prints the remaining items in the set, which are all the missing items of the array.






        share|improve this answer


























          6














          This code uses a HashSet:



          public static void main(String args) {
          int numbers = {9, 6, 4, 5, 7, 0, 1};
          Arrays.sort(numbers);
          HashSet<Integer> set = new HashSet<>();

          for (int i = numbers[0]; i < numbers[numbers.length - 1]; i++) {
          set.add(i);
          }

          for (int i = 0; i < numbers.length; i++) {
          set.remove(numbers[i]);
          }

          for (int x : set) {
          System.out.print(x + " ");
          }
          }


          will print:



          2 3 8 



          Here is how it works:

          1. Adds all numbers from the minimum number of the array to the maximum number of the array to the set.

          2. Iterates through the array and removes every item of the array from the set.

          3. Prints the remaining items in the set, which are all the missing items of the array.






          share|improve this answer
























            6












            6








            6






            This code uses a HashSet:



            public static void main(String args) {
            int numbers = {9, 6, 4, 5, 7, 0, 1};
            Arrays.sort(numbers);
            HashSet<Integer> set = new HashSet<>();

            for (int i = numbers[0]; i < numbers[numbers.length - 1]; i++) {
            set.add(i);
            }

            for (int i = 0; i < numbers.length; i++) {
            set.remove(numbers[i]);
            }

            for (int x : set) {
            System.out.print(x + " ");
            }
            }


            will print:



            2 3 8 



            Here is how it works:

            1. Adds all numbers from the minimum number of the array to the maximum number of the array to the set.

            2. Iterates through the array and removes every item of the array from the set.

            3. Prints the remaining items in the set, which are all the missing items of the array.






            share|improve this answer












            This code uses a HashSet:



            public static void main(String args) {
            int numbers = {9, 6, 4, 5, 7, 0, 1};
            Arrays.sort(numbers);
            HashSet<Integer> set = new HashSet<>();

            for (int i = numbers[0]; i < numbers[numbers.length - 1]; i++) {
            set.add(i);
            }

            for (int i = 0; i < numbers.length; i++) {
            set.remove(numbers[i]);
            }

            for (int x : set) {
            System.out.print(x + " ");
            }
            }


            will print:



            2 3 8 



            Here is how it works:

            1. Adds all numbers from the minimum number of the array to the maximum number of the array to the set.

            2. Iterates through the array and removes every item of the array from the set.

            3. Prints the remaining items in the set, which are all the missing items of the array.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 17 at 22:28









            forpas

            7,4251419




            7,4251419

























                4














                replace the else clause to be:



                for(int j=numbers[i-1] + 1; j <= numbers[i] - 1; j++) {
                System.out.println( "Missing number is " + ( j ) );
                }


                let's examine the case: {9 ,6 ,4 ,5 ,7 ,0 , 1}
                after sorting it will be: {0, 1, 4, 5, 6, 7, 9}
                now if i is at index 2 it finds the difference between numbers[i] and numbers[i-1] to be not equal 1 (4 - 1 = 3), now you need ALL the numbers between 1 and 4 which are 2, 3 and thus you must loop from numbers[i-1] to numbers[i] (exclusive) in order to achieve this.



                The complexity of this code is big O of N (O(N)), where N is the biggest element in your array.






                share|improve this answer




























                  4














                  replace the else clause to be:



                  for(int j=numbers[i-1] + 1; j <= numbers[i] - 1; j++) {
                  System.out.println( "Missing number is " + ( j ) );
                  }


                  let's examine the case: {9 ,6 ,4 ,5 ,7 ,0 , 1}
                  after sorting it will be: {0, 1, 4, 5, 6, 7, 9}
                  now if i is at index 2 it finds the difference between numbers[i] and numbers[i-1] to be not equal 1 (4 - 1 = 3), now you need ALL the numbers between 1 and 4 which are 2, 3 and thus you must loop from numbers[i-1] to numbers[i] (exclusive) in order to achieve this.



                  The complexity of this code is big O of N (O(N)), where N is the biggest element in your array.






                  share|improve this answer


























                    4












                    4








                    4






                    replace the else clause to be:



                    for(int j=numbers[i-1] + 1; j <= numbers[i] - 1; j++) {
                    System.out.println( "Missing number is " + ( j ) );
                    }


                    let's examine the case: {9 ,6 ,4 ,5 ,7 ,0 , 1}
                    after sorting it will be: {0, 1, 4, 5, 6, 7, 9}
                    now if i is at index 2 it finds the difference between numbers[i] and numbers[i-1] to be not equal 1 (4 - 1 = 3), now you need ALL the numbers between 1 and 4 which are 2, 3 and thus you must loop from numbers[i-1] to numbers[i] (exclusive) in order to achieve this.



                    The complexity of this code is big O of N (O(N)), where N is the biggest element in your array.






                    share|improve this answer














                    replace the else clause to be:



                    for(int j=numbers[i-1] + 1; j <= numbers[i] - 1; j++) {
                    System.out.println( "Missing number is " + ( j ) );
                    }


                    let's examine the case: {9 ,6 ,4 ,5 ,7 ,0 , 1}
                    after sorting it will be: {0, 1, 4, 5, 6, 7, 9}
                    now if i is at index 2 it finds the difference between numbers[i] and numbers[i-1] to be not equal 1 (4 - 1 = 3), now you need ALL the numbers between 1 and 4 which are 2, 3 and thus you must loop from numbers[i-1] to numbers[i] (exclusive) in order to achieve this.



                    The complexity of this code is big O of N (O(N)), where N is the biggest element in your array.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 17 at 22:47

























                    answered Dec 17 at 22:10









                    Mis94

                    7251720




                    7251720























                        1














                        There are a lot of questions which are unanswered here. For Example, Does an array always start with zero?, What is the maximum possible size? etc.



                        Here is a simple way to approach this problem,




                        • Find the maximum number in your set.

                        • Create an empty boolean array of the length as that of the max number you found in the last step plus one.

                        • Scan your original set and set the value of your new boolean array at the index equal to the number in your original set as true.

                        • Finally scan your boolean array to find and pront all the indices with value false.


                        Example:



                        Original Set: {1,0,3}





                        • Step 1: Maximum number in the set = 3


                        • Step 2: Boolean Array with Length = 3+1 --> {false, false, false, false}


                        • Step 3: While scanning original set and setting values in the boolean array this will be the final state --> {true, true, false, true}


                        • Step 4: You'll finally scan the boolean array to print 2 since this it is only index which has value = false






                        share|improve this answer





















                        • My code shown that it is not mandatory to start an array from zero. As the elements depends upon array index. if(numbers[0]=0 ) then its ok if (numbers[0]=5) then it is also ok. All it is matter that the array is a sorted array.
                          – Encipher
                          Dec 17 at 22:13
















                        1














                        There are a lot of questions which are unanswered here. For Example, Does an array always start with zero?, What is the maximum possible size? etc.



                        Here is a simple way to approach this problem,




                        • Find the maximum number in your set.

                        • Create an empty boolean array of the length as that of the max number you found in the last step plus one.

                        • Scan your original set and set the value of your new boolean array at the index equal to the number in your original set as true.

                        • Finally scan your boolean array to find and pront all the indices with value false.


                        Example:



                        Original Set: {1,0,3}





                        • Step 1: Maximum number in the set = 3


                        • Step 2: Boolean Array with Length = 3+1 --> {false, false, false, false}


                        • Step 3: While scanning original set and setting values in the boolean array this will be the final state --> {true, true, false, true}


                        • Step 4: You'll finally scan the boolean array to print 2 since this it is only index which has value = false






                        share|improve this answer





















                        • My code shown that it is not mandatory to start an array from zero. As the elements depends upon array index. if(numbers[0]=0 ) then its ok if (numbers[0]=5) then it is also ok. All it is matter that the array is a sorted array.
                          – Encipher
                          Dec 17 at 22:13














                        1












                        1








                        1






                        There are a lot of questions which are unanswered here. For Example, Does an array always start with zero?, What is the maximum possible size? etc.



                        Here is a simple way to approach this problem,




                        • Find the maximum number in your set.

                        • Create an empty boolean array of the length as that of the max number you found in the last step plus one.

                        • Scan your original set and set the value of your new boolean array at the index equal to the number in your original set as true.

                        • Finally scan your boolean array to find and pront all the indices with value false.


                        Example:



                        Original Set: {1,0,3}





                        • Step 1: Maximum number in the set = 3


                        • Step 2: Boolean Array with Length = 3+1 --> {false, false, false, false}


                        • Step 3: While scanning original set and setting values in the boolean array this will be the final state --> {true, true, false, true}


                        • Step 4: You'll finally scan the boolean array to print 2 since this it is only index which has value = false






                        share|improve this answer












                        There are a lot of questions which are unanswered here. For Example, Does an array always start with zero?, What is the maximum possible size? etc.



                        Here is a simple way to approach this problem,




                        • Find the maximum number in your set.

                        • Create an empty boolean array of the length as that of the max number you found in the last step plus one.

                        • Scan your original set and set the value of your new boolean array at the index equal to the number in your original set as true.

                        • Finally scan your boolean array to find and pront all the indices with value false.


                        Example:



                        Original Set: {1,0,3}





                        • Step 1: Maximum number in the set = 3


                        • Step 2: Boolean Array with Length = 3+1 --> {false, false, false, false}


                        • Step 3: While scanning original set and setting values in the boolean array this will be the final state --> {true, true, false, true}


                        • Step 4: You'll finally scan the boolean array to print 2 since this it is only index which has value = false







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Dec 17 at 22:08









                        user2004685

                        7,30642143




                        7,30642143












                        • My code shown that it is not mandatory to start an array from zero. As the elements depends upon array index. if(numbers[0]=0 ) then its ok if (numbers[0]=5) then it is also ok. All it is matter that the array is a sorted array.
                          – Encipher
                          Dec 17 at 22:13


















                        • My code shown that it is not mandatory to start an array from zero. As the elements depends upon array index. if(numbers[0]=0 ) then its ok if (numbers[0]=5) then it is also ok. All it is matter that the array is a sorted array.
                          – Encipher
                          Dec 17 at 22:13
















                        My code shown that it is not mandatory to start an array from zero. As the elements depends upon array index. if(numbers[0]=0 ) then its ok if (numbers[0]=5) then it is also ok. All it is matter that the array is a sorted array.
                        – Encipher
                        Dec 17 at 22:13




                        My code shown that it is not mandatory to start an array from zero. As the elements depends upon array index. if(numbers[0]=0 ) then its ok if (numbers[0]=5) then it is also ok. All it is matter that the array is a sorted array.
                        – Encipher
                        Dec 17 at 22:13











                        0














                        int numbers = { 11, 6, 4, 5, 7, 1 };
                        Arrays.sort(numbers);
                        int numbersArrayIndex = 0;
                        for (int i = 0; i < numbers[numbers.length - 1]; i++) {
                        if (i == numbers[numbersArrayIndex]) {
                        numbersArrayIndex++;
                        }
                        else {
                        System.out.println(i);
                        }
                        }





                        share|improve this answer


























                          0














                          int numbers = { 11, 6, 4, 5, 7, 1 };
                          Arrays.sort(numbers);
                          int numbersArrayIndex = 0;
                          for (int i = 0; i < numbers[numbers.length - 1]; i++) {
                          if (i == numbers[numbersArrayIndex]) {
                          numbersArrayIndex++;
                          }
                          else {
                          System.out.println(i);
                          }
                          }





                          share|improve this answer
























                            0












                            0








                            0






                            int numbers = { 11, 6, 4, 5, 7, 1 };
                            Arrays.sort(numbers);
                            int numbersArrayIndex = 0;
                            for (int i = 0; i < numbers[numbers.length - 1]; i++) {
                            if (i == numbers[numbersArrayIndex]) {
                            numbersArrayIndex++;
                            }
                            else {
                            System.out.println(i);
                            }
                            }





                            share|improve this answer












                            int numbers = { 11, 6, 4, 5, 7, 1 };
                            Arrays.sort(numbers);
                            int numbersArrayIndex = 0;
                            for (int i = 0; i < numbers[numbers.length - 1]; i++) {
                            if (i == numbers[numbersArrayIndex]) {
                            numbersArrayIndex++;
                            }
                            else {
                            System.out.println(i);
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered yesterday









                            tech

                            364




                            364






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53823608%2ffind-out-n-numbers-of-missing-elements-from-an-array-in-java%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                Alcedinidae

                                RAC Tourist Trophy