Find 2 missing numbers in an array of integers with two missing values

前端 未结 12 905
梦谈多话
梦谈多话 2021-01-30 09:33

How do you do this? The values are unsorted but are of [1..n] Example array [3,1,2,5,7,8]. Answer: 4, 6

I saw this solution in

12条回答
  •  情歌与酒
    2021-01-30 10:19

    Below is the generic answer in java code for any number of missing numbers in a given array
    //assumes that there are no duplicates
    a = [1,2,3,4,5]
    b = [1,2,5]
    a-b=[3,4]
    
    public list find(int[] input){
      int[] a= new int[] {1,2,3,4,5};//create a new array without missing numbers
      List l = new ArrayList();//list for missing numbers
      Map m = new HashMap();
    
      //populate a hashmap with the elements in the new array
      for(int i=0;i

提交回复
热议问题