Skip to content
Advertisement

Returning a subset that has a duplicate

My question is how to detect the duplicate on one subset of array that does not exist in the other substring?

For example Input: arr1 = 1,2,3 arr2 = 1,1

Output: Arr2 is not a subset of Arr1

So far everything else works fine and how I want it to but this duplicate one keeps returning that it is a subset. I have tried removing an element by putting it inside the for loop but as an else if but I don’t think it was it. Removing the same element on both strings and then doing a loop again to

package com.company;

public class RandomArrayFunctionalities
{
    public boolean subSet(int Array1[], int Array2[], int sizeOne, int sizeTwo)
    {
        //
        int i, j;

        for(i = 0; i < sizeTwo; i++)
        {
            for(j = 0; j< sizeOne; j++)
            {
                if(Array2[i] == Array1[j])
                {
                    for(i = 0; i < sizeOne - 1; i++)
                    {

                        Array1[i] = Array1[i + 1];

                        /* Decrement array size by 1 */
                        sizeOne--;

                        for(j = 0; j < sizeTwo-1; j++)
                        {
                            sizeTwo--;

                            if(Array2[i] == Array1[j])
                            {

                                break;
                            }
                        }
                        if(j == sizeOne)
                        {
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    }

                    break;
                }
            }
            if (j == sizeOne)
            {
                return false;
            }
        }
        return true;
    }
}

public static void main(String[] args)
    {
        RandomArrayFunctionalities ranMethod = new RandomArrayFunctionalities();

        int arr1[] = {1, 2, 3};
        int arr2[] = {1,1};

        int sizeOne = arr1.length;;
        int sizeTwo = arr2.length;;

        if(ranMethod.subSet(arr1, arr2, sizeOne, sizeTwo))
        {
            System.out.println("nArray 2 is a subset of array 1n");
        }
        else
        {
            System.out.println("nArray 2 is not a subset of array 1n");
        }
    }

Advertisement

Answer

I think one of the problems you have in your implementation is to not remove the element in the main list when it is in the subset list.
So when you check the second “1”, it still exists in the main list.

I would do it as follows. There should be more checks whether the subset list is bigger than the main list but it is easy to add.

public static void main(final String[] args) {
    final List<String> main = Arrays.asList("1", "2", "3");
    final List<String> subset = Arrays.asList("1", "2");
    final List<String> notSubset = Arrays.asList("1", "1");

    displaySubsetOrNot(main, subset);
    displaySubsetOrNot(main, notSubset);
}

private static void displaySubsetOrNot(final List<String> main, final List<String> subset) {
    if (isSubset(main, subset)) {
        System.out.println("subset");
    } else {
        System.out.println("not subset");
    }
}

private static boolean isSubset(final List<String> mainList, final List<String> subsetList) {
    final List<String> mainListCopy = new ArrayList<>(mainList);
    for (final String element : subsetList) {
        if (!mainListCopy.remove(element)) {
            return false;
        }
    }
    return true;
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement