SEB/Daily Coding

[DailyCoding] 24 | isSubsetOf

kexon 2022. 8. 25. 21:35

✏️ Description

Return whether the sample is a subset of the base by receiving two arrays

  • input
    • base: temporary Array with element of int
    • sample: temporary Array with element of int
  • output: boolean
  • caution: no duplicates
  • example of in/output
int[] base = new int[]{1, 2, 3, 4, 5};
int[] sample = new int[]{1, 3};
boolean output = isSubsetOf(base, sample);
System.out.println(output); // --> true

sample = new int[]{6, 7};
output = isSubsetOf(base, sample);
System.out.println(output); // --> false

base = new int[]{10, 99, 123, 7};
sample = new int[]{11, 100, 99, 123};
output = isSubsetOf(base, sample);
System.out.println(output); // --> false

📝 Flow

1. get one by one
	1-1. from sample to sampleArr
	1-2. from base to baseArr
2. if it matches then 
	- change the flag to false and navigate to the next sampleArr
3. if doesn't matches then
	- flag is true => return false
4. if get no return false => return true

📚 TIL

  • Implement Array instead List.contains method of List
  • flag

👩🏻‍💻 Implementation

for (int intSample : sample) {
    boolean flag = true;

    for (int intBase : base) {
        if (intSample == intBase) {
            flag = false;
            break;
        }
    }

    if (flag) return false;
}

return true;