Finding missing elements in an array

前端 未结 9 1333
我寻月下人不归
我寻月下人不归 2020-12-23 10:43

Given you have an array A[1..n] of size n, it contains elements from the set {1..n}. However, two of the elements are missing, (and perhaps two of the array elements are rep

9条回答
  •  北荒
    北荒 (楼主)
    2020-12-23 11:16

    As we know we are looking for elements between 1 to N Create a Hash set containing 1 to N.

    foreach(int i in input)
    {
       if(hashset.contains(i))
       {
          hashset.delete(i);
       }
    }
    
    return "remaining elements in Hashset.";
    

    The remaining elements in Hashset are the missing elements.

提交回复
热议问题