# Binary operations with unordered sets

In this post I will do algorithmization of the array operations and describe the operations.

Summary:

• I. Intersection of sets
• II. Différence of sets
• III. Union of sets
• IV. Symmetric difference of sets
• conclusion

## I. Intersection of sets

The intersection of two sets A and B is such a sets that contains all elements of A that also belong to B (or equivalently, all elements of B that also belong to A), but no other elements. The time complexity of its calculation is `O(m*n)`, where `m` and `n` are the lengths of A and B sets.

``````function intersection(A, B)
{
var m = A.length, n = B.length, c = 0, C = [];
for (var i = 0; i < m; i++)
{
var j = 0, k = 0;
while (B[j] !== A[ i ] && j < n) j++;
while (C[k] !== A[ i ] && k < c) k++;
if (j != n && k == c) C[c++] = A[ i ];
}
return C;
}``````

For example,

``intersection ([1,2,3,7,9],[4,5,7,2,1,5]); // Output: [1,2,7]``

## II. Complement of sets

A complement of B in A is a set which contains all the elements of A which does not belong to B. The time complexity is `O(m*n)`, where `m` and `n` are the lengths of the operated sets.

``````function diff(A, B)
{
var M = A.length, N = B.length, c = 0, C = [];
for (var i = 0; i < M; i++)
{
var j = 0, k = 0;
while (B[j] !== A[ i ] && j < N) j++;
while (C[k] !== A[ i ] && k < c) k++;
if (j == N && k == c) C[c++] = A[ i ];
}
return C;
}``````

For example,

``````diff([1,2,3,7,9],[4,5,7,2,1,5]); // Output [3,9]
diff([4,5,7,2,1,5], [1,2,3,7,9]); // Output [4,5]``````

## III. Union of sets

The union of two sets A and B is the set of elements which are in A, in B, or in both A and B. The time complexity is `O(m*n)`, where `m` and `n` are the lengths of A and B sets.

``````function sum(A, B)
{
var M = A.length, N = B.length, count = 0, C = [];
C = A;
count = M;
var cnt = 0;
for (var i=0;i``````

For example,

``````sum([1,2,3,7,9],[4,5,7,2,1,5]); // Output [1,2,3,7,9,4,5]
sum([4,5,7,2,1,5],[1,2,3,7,9]); // Output [4,5,7,2,1,5,3,9]``````

## IV. Symmetric difference of sets

The symmetric difference of two sets is the set of elements which are in either of the sets and not in their intersection. The time complexity is `O(2m*n)`, where `m` and `n` are the lengths of operated sets.

In fact, this is complement of arrays, first `A \ B` and then `B \ A`.

``````function symmetricDiff(A,B)
{
var M = A.length, N = B.length, c = 0, C = [];
for (var i = 0; i < M; i++)
{
var j = 0, k = 0;
while (B[j] !== A[ i ] && j < N) j++;
while (C[k] !== A[ i ] && k < c) k++;
if (j == N && k == c) C[c++] = A[ i ];
}
for (var i = 0; i < N; i++)
{
var j = 0, k = 0;
while (A[j] !== B[ i ] && j < M) j++;
while (C[k] !== B[ i ] && k < c) k++;
if (j == M && k == c) C[c++] = B[ i ];
}
return C;
}``````

For example,

``````symmetricDiff([1,2,3,7,9],[4,5,7,2,1,5]);// Output: [3,9,4,5]
symmetricDiff([1,2,3,4,5],[3,4,5,6,7]); // Output: [1,2,6,7]``````

## Conclusion

In practise this can be used, for example, to calculate which followers 2 users share in a social network.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7836596,"math_prob":0.9998165,"size":2880,"snap":"2021-43-2021-49","text_gpt3_token_len":1071,"char_repetition_ratio":0.11091794,"word_repetition_ratio":0.4379085,"special_character_ratio":0.44756943,"punctuation_ratio":0.25179857,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9998087,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,2,null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-05T10:58:09Z\",\"WARC-Record-ID\":\"<urn:uuid:e513b114-7494-4d30-ad53-e4a99c8061d2>\",\"Content-Length\":\"8343\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:df874761-519c-403b-a41f-9cd8819d50b8>\",\"WARC-Concurrent-To\":\"<urn:uuid:a234a217-7397-4dff-9c54-19c3d33218f9>\",\"WARC-IP-Address\":\"161.35.236.32\",\"WARC-Target-URI\":\"https://frontendbabel.info/articles/binary-operations-with-irregular-sets/\",\"WARC-Payload-Digest\":\"sha1:P42GK2HT2FERT2Y6ILX6NJFPNFIUD7ZW\",\"WARC-Block-Digest\":\"sha1:ZPHVJICPQT72UTQN53CHQBI2CLCIFXVR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363157.32_warc_CC-MAIN-20211205100135-20211205130135-00521.warc.gz\"}"}