Pardeep Kr
Pardeep Kr

Reputation: 497

How to get the different elements of two arrays

I have two arrays of integers . suppose A[5] = {1,2,3,4,5} and B[6] = [1,2,3,6,7,8] .Now If some elements is present in A But not is B , i want to remove that element from A . But if some element is present in B But not in A , I want to add that element in A.If elements are there in both Arrays,then leave them as it is. that How can i efficiently achieve this.

For the example above the final output will be :-

A[] = {1,2,3,6,7,8} or we can save the output in separate Array as well. C[] = {1,2,3,6,7,8}

Upvotes: 0

Views: 78

Answers (1)

aaalex88
aaalex88

Reputation: 649

  1. If some elements is present in A But not is B , i want to remove that element from A
  2. But if some element is present in B But not in A , I want to add that element in A.
  3. If elements are there in both Arrays,then leave them as it is.

So, every element from B should be in result, and no element that isn't in B shouldn't be in result.

How can i efficiently achieve this.

I think, you can just reset A and copy elements of B to it.

Upvotes: 2

Related Questions