0

I have two arrays:

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = [2, 4, 6, 8, 10]

How is it possible to make the output look like this?

list3 = [1, 3, 5, 7, 9]

list3[i] = list1[i] - list2[i];

Unfortunately this does not work for me because the two arrays are not same length.

import java.util.Arrays;
import java.util.Scanner;

public class Infinity {

    public static int[] InitialiseStudents(int[] students) {

        for (int i = 0; i<students.length; i++){
            students[i] = i+1;
        }
        return students;
    }

    public static int[] AssignJobs (int[] NumberStudents) {
        int StudCount = NumberStudents.length;
        int[] Array = NumberStudents;

        //loop for every day(t), starting at the second day
        for (int t = 2; t <= StudCount; t++) {
            System.out.println("Tag" + t);


            int[] copy = new int[5];

            for (int i = t-1, j=0; j < 5; i+=t) {
                    copy[j++] = Array[i];
            }
            System.out.println("-------------");
            System.out.println(Arrays.toString(copy));
            System.out.println("_________");
            break;
        }
        return Array;
    }

    public static void main( String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter Number of Students: ");
        //int n = scan.nextInt();
        int n = 10;

        int[] students = new int[n];

        InitialiseStudents(students);

        students = AssignJobs(students);
        System.out.println(Arrays.toString(students));

    }
}

To make to task VERY clear I just show you everything I have done and the context. This is my homework which I am curently working. The task is...

Initially, a numbering 1, 2, 3, 4, . . . of the students is determined. Then it will be that every second (start counting with the first student) becomes Garbage officer (these are the students with the numbers 2, 4, 6, 8, . . . ), from the rest (students 1, 3, 5, 7, . . . ) every third person becomes a refrigerator representative (these are students 5, 11, 17, 23, . . . ), of the rest (students 1, 3, 7, 9, . . . ) every fourth . . . , from which then we can think of something for each k-th and from the rest for every (k+1)-th etc. Apparently, some of the residents (students 1, 3, 7) omitted during distribution and do not have to complete any of the tasks that arise. Numbers of these students are called Omitted Numbers. Program an application OmittedNumbers that exactly the Omitted Numbers an area 1; : : : ;N retrieves and prints, where N is passed on the command line will. Only use methods that are iterative.

12
  • 1
    It doesn't work because you can't use the subtraction operator on arrays. Use Set and Set.removeAll instead. Commented Jan 18, 2023 at 2:47
  • 1
    @StephenC it looks like a set difference to me -- otherwise wouldn't the result be [-1, -2, -3, ... ? Commented Jan 18, 2023 at 3:06
  • @StephenC According to the result, the OP wants a set-theoretical difference between the two arrays. Commented Jan 18, 2023 at 3:06
  • 1
    OP - Please describe in words what >you< mean by "subtract an array from an array". Commented Jan 18, 2023 at 3:12
  • 1
    I can see that. I'm asking if it's guaranteed. Commented Jan 18, 2023 at 3:40

4 Answers 4

1

Solution # 1

Convert your arrays to list

List<Integer> list1 = Arrays.asList(array1);
List<Integer> list2 = Arrays.asList(array2);

For List, you can use removeAll function

list1.removeAll(list2);
System.out.println(list1)

Solution # 2

Traverse through each index and remove items if same as follow

int[] array1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] array2 = new int[]{2, 4, 6, 8, 10};

List<Integer> result = new ArrayList<>();

for (int i = 0; i < array1.length; i++) {
    for (int j = 0; j < array2.length; j++) {
        if (array1[i] == array2[j]) {
            result.add(array1[i]);
        }
    }
}

// convert list to array (if needed)
Integer[] resultArray = result.toArray(new Integer[0]);
Sign up to request clarification or add additional context in comments.

Comments

1

Here's a simple implementation:

import java.util.ArrayList;
import java.util.List;

record ChoiceResult(List<Integer> chosen, List<Integer> remainder) {}

public class Choose {
    static ChoiceResult choose(int interval, List<Integer> candidates) {
        List<Integer> chosen = new ArrayList<>();
        List<Integer> remainder = new ArrayList<>();
        for (int i = 0; i < candidates.size(); i++) {
            if ((i+1) % interval == 0) {
                chosen.add(candidates.get(i));
            } else {
                remainder.add(candidates.get(i));
            }
        }
        return new ChoiceResult(chosen, remainder);
    }

    public static void main(String[] args) {
        List<Integer> students = List.of(1,2,3,4,5,6,7,8,9,10);
        ChoiceResult garbage = choose(2, students);
        ChoiceResult fridge = choose(3, garbage.remainder());
        System.out.println("Garbage: " + garbage.chosen());
        System.out.println("Fridge: " + fridge.chosen());
    }
}

It has the feature of working with an immutable List for the input to the function.

Comments

0

You can use double-layer for loop filtering,The following is just a sample code, you need to think about the details(For example, how to improve the efficiency of calculation, because the efficiency of double-layer for loop is very low).

public static void main(String[] args) {
    int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int[] arr2 = {2, 4, 6, 8, 10};

    List<Integer> rt = new ArrayList<>();
    for (int i = 0; i < arr1.length; i++) {
        boolean flag = false;
        for (int j = 0; j < arr2.length; j++) {
            if(arr1[i] == arr2[j]) {
                flag = true;
                break;
            }
        }
        if (flag == false) {
            rt.add(arr1[i]);
        }
    }
    System.out.println(rt);

    Integer[] finalArr = rt.toArray(new Integer[rt.size()]);

    for (int i = 0; i < finalArr.length; i++) {
        System.out.println(finalArr[i]);
    }
}

Comments

0

Collection framework supports union/intersection at base level and just utilize it.

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
        List<Integer> list2 = new ArrayList<>(List.of(2, 4, 6, 8, 10));
        list1.removeAll(list2);
        System.out.println(list1);
    }
}

Using Stream API:

import java.util.List;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        List<Integer> list1 = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        List<Integer> list2 = List.of(2, 4, 6, 8, 10);
        List<Integer> list3 = list1.stream()
                .filter(i -> !list2.contains(i))
                .collect(Collectors.toList());
        System.out.println(list3);
    }
}

collection methods reference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.