# Count elements present in first array but not in second in C++

We are given an array of any size containing integer elements and the task is to calculate the count of elements that are present in the first array but not in the second array..

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

## For example

Input− int arr_1[] = {1, 2, 3, 4}
Int arr_2[] = {1, 5, 6, 7, 8}
Output − count is 3

Explanation − In the given arr_1 we have elements 1, 2, 3 and 4 and in the arr_2 we have 1, 5, 6,7 and 8. The element 1 is present in both the arrays therefore, we will not count it and hence the count becomes 3.

Input − int arr_1[] = {10, 20, 30, 40, 50}
Int arr_2[] = {10, 20, 30, 60}
Output − count is 2

Explanation − In the given arr_1 we have elements 10, 20, 30, 40 and 50 and in the arr_2 we have 10, 20, 30 and 60. The elements 10, 20 and 30 are present in both the arrays therefore, we will not count them and hence the count becomes 2.

## Approach used in the below program is as follows

• Create two arrays let’s say, arr_1[] and arr_2[]

• Calculate the length of both the arrays using the length() function that will return an integer value as per the elements in an array.

• Take a temporary variable that will store the count of elements present only in the first array.

• Create an unordered map let’s say up

• Start loop for with i to 0 till i less then the size of arr_1

• Increment up[arr_1[i]] with 1

• Start another loop for with i to 0 till i less then the size of arr_2

• Inside the loop, check if up.find(arr_2[i])!=up.end() and up[arr_2[i]]!=0

• decrement up[arr_2[i]] by 1

• Start another loop for with i to 0 till i less then the size of arr_1

• Inside the loop, check if up[arr_1[i]!=0

• Then, Increment the count by 1 and set up[arr_1[i]] = 0

• Return the count

• Print the result .

## Example

Live Demo

#include <iostream>
#include<unordered_map>
using namespace std;
int elements_count(int arr_1[], int arr_2[], int m, int n){
bool f = false;
int result = 0;
// map to store frequency of elements present in a
unordered_map<int, int> up;
for (int i = 0; i < m; i++){
up[arr_1[i]]++;
}
// check if the elements of b
// is present in b or not
for (int i = 0; i < n; i++)
if (up.find(arr_2[i]) != up.end() && up[arr_2[i]] != 0){
up[arr_2[i]]--;
}
// count the elements of a whose
// frequency is more than b
for (int i = 0; i < m; i++) {
if (up[arr_1[i]] != 0){
result++;
up[arr_1[i]] = 0;
}
}
return result;
}
// Main function
int main(){
int arr_1[] = { 2, 4, 4, 6, 6, 6, 8, 9 };
int arr_2[] = { 2, 2, 4, 6, 6 };
int m = sizeof(arr_1)/sizeof(arr_1);
int n = sizeof(arr_2)/sizeof(arr_2);
cout <<"count is "<<elements_count(arr_1, arr_2, m, n);
return 0;
}

## Output

If we run the above code we will get the following output −

count is 4

Updated on: 15-May-2020

69 Views 