Java Solution


  • 0
    R
    public List<Integer> findDisappearedNumbers(int[] nums) {
    
    
            // the TreeSet used to get the element sorted and reomve the duplicates
            Set<Integer> set = new TreeSet();
    
            for (int i = 0; i < nums.length; i++) {
                set.add(nums[i]);
            }
    
            // build the new array which will have matching elements at their index  location
            // the missing number will have 0 at their index
            int[] newNums = new int[nums.length];
            for (Integer x : set) {
                newNums[x - 1] = x;
            }
    
            // now build the final result list
            // which will have missing elements
            List<Integer> list = new ArrayList(nums.length);
    
            for (int j = 0; j < newNums.length; j++) {
                if (newNums[j] == 0) {
                    // not present so add to the list
                    list.add(j + 1);
                }
            }
    
            return list;
    
        }
    
    
    

    Time Complexity: O(n)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.