Automation Using Selenium Webdriver

Tuesday 15 November 2016

Java Program to find missing numbers in an array

  • To find missing numbers in an array first we need to make sure that array is sorted.
  • After sorting we need to check that array each element with next element then we can find the difference.
  • if Array is not sorted :To sort array use Arrays.sort(array);
  • If difference is 1 then no need to do any thing because numbers are in order.
  • If difference is not equal to 1 then we need to print all those numbers or pick those numbers and place it in one array.
  • this would be the logic to find missing numbers in an array
  • Here there may be a chance of array not starts with 1 then we need to check first itself whether array starts with 1 or not if not we need to print 1 to starting element of array.
  • for example int a[]={4,5,6,8}; then we need to print 1 2 3  7.
Lets see a java example program to find missing numbers in an array.


 

 Lets see a java example program to find missing numbers in an array.

  1. package arraysInterviewQuestions;
  2. public class PrintMissingNumbers {
  3.  
  4. private static void findMissingNumber(int[] number){
  5.  
  6.         // take max length as last number in array
  7.     int k[] = new int[number[number.length-1]];
  8.         
  9.   int m=0;
  10.  
  11.   if(number[0]!=1){
  12.    for (int x = 1; x < number[0]; x++) {
  13.        k[m] =  x;
  14.        m++;
  15.        }
  16.   }
  17.         
  18.  for (int i = 0; i < number.length -1; i++) {
  19.     
  20.     int j = i+1;
  21.     int difference = number[j] - number[i] ;
  22.             
  23.             
  24.    if(difference != 1 ){
  25.         
  26.   for (int x = 1; x < difference; x++) {
  27.  
  28.           k[m] = number[i] + x;
  29.            m++;
  30.     
  31. }
  32.             
  33.  }
  34.  }
  35.         
  36. System.out.println("missing numbers in array ::");
  37.         
  38. for (int l = 0; l < m ; l++) {
  39.     System.out.println( k[l]+" ");
  40. }
  41. }
  42.  public static void main(String[] args) {
  43.         
  44.    int a[]= {2,4,6,9,10,20};
  45.  
  46.    //if Array is not sorted :To sort array use Arrays.sort(a); 
  47.  
  48.   findMissingNumber(a);
  49.  
  50.    
  51. }
  52. }
 
Missing%2Bnumbers%2Bin%2Barray


  • Change the array in main method and practice try to find missing elements in an array that contains 1 to 100 numbers.

  • You Might Like

How to find uppercase letters in a string in java


  • How can we count upper case letter in String  java ?
  • How to find uppercase letters in String  java?
  • How to find capital letters in string in java?
  • How u discover the capital letter given sentence?
  • Yes We can find uppercase or capital letters in a String using isUpperCase() method of Character class in java
  • In order to find number of uppercase letters in a string of all capital letters in a string we need to iterate all characters of a string in a loop and check individual characters are uppercase letters or not using Character class provided isUpperCase() method.

Program #1: Java example program to find all capital letters / Uppercase letters in a String 



  1. package findUppercaseletters.String;
  2. public class FinfUppercaseLetters {
  3.  
  4.     /**
  5.      * 
  6.      */
  7.  
  8.  public static void main(String[] args) {
  9.         
  10.         
  11.   String str= "How to Print Uppercase Letters in Java";
  12.  
  13.     for (int i = 0; i < str.length(); i++) {
  14.     
  15.             if(Character.isUpperCase(str.charAt(i))){    
  16.             System.out.println(str.charAt(i));
  17.             }
  18.             
  19.  }
  20.  
  21. }
  22.  
  23. }

 Output:

  1. H
  2. P
  3. U
  4. L
  5. J


Sort ArrayList in descending order

Descending order:

  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3.  import java.util.Collections;
  4. import java.util.Comparator;
  5.  
  6. public class SortArrayListDesc {
  7.  
  8.      public static void main(String[] args) {
  9.  
  10.             //create an ArrayList object
  11.             ArrayList arrayList = new ArrayList();
  12.  
  13.             //Add elements to Arraylist
  14.             arrayList.add(1);
  15.             arrayList.add(2);
  16.             arrayList.add(3);
  17.            arrayList.add(4);
  18.             arrayList.add(5);
  19.             arrayList.add(6);
  20.  
  21.             /*
  22.            Use static Comparator reverseOrder() method of Collections 
  23.         utility class to get comparator object
  24.            */
  25.  
  26.          Comparator comparator = Collections.reverseOrder();
  27.  
  28.           System.out.println("Before sorting  : "  + arrayList);
  29.         
  30.  
  31.            /*
  32.               use
  33.               static void sort(List list, Comparator com) method of Collections class.
  34.             */
  35.  
  36.             Collections.sort(arrayList,comparator);
  37.             System.out.println("After sorting  : + arrayList);
  38.  
  39.        }
  40.     }
     

  1. OutPut:
  2. Before sorting  : [1, 2, 3, 4, 5, 6]
  3. After sorting  : [6, 5, 4, 3, 2, 1]

  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3.  import java.util.Collections;
  4. import java.util.Comparator;
  5.  
  6. public class SortArrayListAsc{
  7.  
  8.      public static void main(String[] args) {
  9.  
  10.             //create an ArrayList object
  11.             ArrayList arrayList = new ArrayList();
  12.  
  13.             //Add elements to Arraylist
  14.             arrayList.add(10);
  15.             arrayList.add(4);
  16.             arrayList.add(7);
  17.            arrayList.add(2);
  18.             arrayList.add(5);
  19.             arrayList.add(3);
  20.  
  21.           
  22.  
  23.           System.out.println("Before sorting  : "  + arrayList);
  24.         
  25.  
  26.            /*
  27.               use
  28.               static void sort(List list) method of Collections class.
  29.             */
  30.  
  31.             Collections.sort(arrayList);
  32.             System.out.println("After sorting  : + arrayList);
  33.  
  34.        }
  35.     }
     

Ascending order:

  1. OutPut:
  2. Before sorting  : [10, 4, 7, 2, 5, 3]
  3. After sorting  : [2, 3, 4, 5, 7, 10]