0

I need to write a program which defines two arrays of int's and prints all elements of the first array which do not appear in the second, but each value once only, without repetitions (the order of printed values is irrelevant). For example for arrays:

int[] arr = { 4, 3, 4, 3, 6, 7, 4, 8, 2, 9 };
int[] brr = { 2, 3, 6, 8, 1, 5 };

The result should be:

7 4 9

It only use the java.lang. package and it can't create any arrays, collections or Strings.

5
  • This question is so confusing. Do you mean, numbers defined in array 1 ,but not in array 2 ? Commented Mar 25, 2019 at 12:38
  • 3
    What have you tried? (BTW, given the crazy requirements, this sounds like a homework assignment, and we're not really in the business of doing folks' homework for them. We like to help with specific programming problems.) Take a look at stackoverflow.com/help/how-to-ask Commented Mar 25, 2019 at 12:38
  • You can do it by iterating through arr and brr Commented Mar 25, 2019 at 12:41
  • Yep, definitely, numbers that are defined in array 1, but not in array 2 Commented Mar 25, 2019 at 12:49
  • No, it's not HW. This task was on the test, but unfortunately I haven't done this. So now I would like to pass this task just for myself. This is why I'm asking. Commented Mar 25, 2019 at 12:52

5 Answers 5

2

This prints all elements of the first array which do not appear in the second, now you just have to check for repetitions.

public static void compareArrays(int[] arr1, int[] arr2) {
  boolean equal = false;
  int[] printed = null;
  for (int i = 0; i < arr1.length; i++) {
    for (int x = 0; x < arr2.length; x++) {
      if (arr1[i] == arr2[x]) {
        equal = true;
        break;
      } else {
        equal = false;
      }
    }
    if (equal != true) {
      System.out.println(arr1[i]);
    }
  }
}
public static void main(String[] args) {
  int[] arr = { 4, 3, 4, 3, 6, 7, 4, 8, 2, 9 };
  int[] brr = { 2, 3, 6, 8, 1, 5 };
  compareArrays(arr, brr);
}
Sign up to request clarification or add additional context in comments.

Comments

1

I will give you some pointers, but will not provide any code as I've not seen any attempt from your side. You can tackle this problem several ways, have a look and try to implement this yourself. You will learn much more during the process, rather than if someone gives you a complete full-code answer. So here goes:

First method:

  1. Create a new ArrayList that will hold your output.
  2. Iterate through the first array, iterate through the second array. Compare each value.
  3. If value is not present and is not present in the output list (you would need to check for that specifically, so you dont have repetition of values), then add it to output list.
  4. Print output list.

Second method:

  1. Convert both arrays into an ArrayList.

  2. Use the removeAll() method provided with Lists to get difference between the arrays. This will be stored in one of the lists you created earlier.

  3. Remove repetitive items from the list (e.g. using Streams).

Third method:

  1. Create a new ArrayList that will hold your output.

  2. Iterate through values of array1.

  3. Initialize a boolean variable (e.g. call it contains) that will determine whether a value from array1 is present in array2 using an IntStream.

  4. Create if statement - if contains is true, add the value to your output list. Check that your output List already doesn't have the value and only add if it doesn't. Print output list.

Comments

1

Try using this instead:

String[] unique = new HashSet<String>(Arrays.asList(arr)).toArray(new String[0]);

for (int uEach : unique) {
  for (int bEach : brr) {
    if (unique[uEach] == brr[bEach]) {
       ArrayUtils.removeElement(unique, uEach);
    }
  }
};

System.out.print(unique);

Comments

0

You can use to get distanct value from an array:

int[] unique = Arrays.stream(arr).distinct().toArray(); 

Then loop over both arrays and match strings if not found print that:

int i, j, occ;
int[] arr = { 4, 3, 4, 3, 6, 7, 4, 8, 2, 9 };
int[] brr = { 2, 3, 6, 8, 1, 5 };
arr = Arrays.stream(arr).distinct().toArray();
for (i = 0; i < arr.length; i++) {
  occ = 0;
  for (j = 0; j < brr.length; j++) {
    if (arr[i] == brr[j]) {
      occ++;
    }
  }
  if (occ == 0) {
    System.out.println(arr[i]);
  }
}

3 Comments

I didn't downvote you, but I imagine it could be because its not encouraged in SO to give a full code answer to a question that doesn't show an attempt. You answering like this encourages people simply pasting their HW and expecting others to do it for them.
It is not HW, it is exercise from the past test, that I haven't done unfortunately. I had a problem with repetitions. And there is used another array, but this should work without any additional arrays.
@MarkLadyshev that'snot a big problem you can replace the new array with the old one
0
static void findMissing(int a[], int b[]) {
  for (int i = 0; i < a.length; i++) {
    int j;
    for (j = 0; j < b.length; j++) {
      if (a[i] == b[j]) {
        break;
      }
    }
    if (j == b.length) {
      System.out.print(a[i] + " ");
    }
  }
}

2 Comments

This method gives you the numbers which are not in another array yeah, but it will have repetative numbers.But I dont understand why down vote?
Please describe, what was the problem, and how will this snippet solve it, to help others understand this answer. This may also result in more upvotes

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.