Clean solution using arrays - O(n) Time O(1) Space


  • 0
    R

    ...
    public int[] findErrorNums(int[] nums) {
    List<Integer> list = new ArrayList<Integer>();
    int[] array = new int[nums.length];
    for(int i = 0; i<nums.length; i++)
    {
    array[nums[i]-1]++;
    }
    for(int i=0; i<array.length; i++)
    {
    if(array[i]==2)
    {
    list.add(i+1);
    }
    }
    for(int i=0; i<array.length; i++)
    {
    if(array[i]==0)
    {
    list.add(i+1);
    }
    }
    int[] arr = list.stream().mapToInt(i->i).toArray();
    return arr;
    }
    ...


Log in to reply
 

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