Repository: pbiswas101/Mathball (js)

Issue:
Kaprekar Number
### Do the checklist before filing the issue:

- [ ] Is this something you can **debug and fix**?
- [x] Do you have Node.js and all the relevant dev-dependencies installed?
- [ ] Is this a **feature request** that you can add?

###### NOTE: Provide a clear and concise description of the feature that needs to be added! Or if its a bug, then provide the necessary steps to reproduce it.

- *Give your answer below:*

------------

[Function: **M.isKaprekar()**] needs to be added!
For more info:

https://www.geeksforgeeks.org/kaprekar-number/

*M.isKaprekar()* needs to accept a positive integer and all other arguments passed should throw a type error which needs to be handled gracefully! For error handling, you should refer to how other features like M.isArmstrong() is implemented.

Pull request:
Adding union of arrays. Issue #199
### Do the checklist before submitting the PR:

- [x] Have you read the guidelines mentioned in **CONTRIBUTING.md**?
- [x] Have you squashed your commits?

**Q**: What version of *Node.js* you've used in the project? (`e.g. Node v10.11.0`)
**A**:v11.6.0

**Q**: Mention the *Issue Number*! (`e.g. Fixed #8`)
**A**:Added #199 

- *Give additional information regarding the PR below:*

------------


Patch:
diff --git a/src/index.js b/src/index.js
index 28bfa7b..40aad51 100644
--- a/src/index.js
+++ b/src/index.js
@@ -30,7 +30,8 @@ const abs = require('./absolute'),
 	pow = require('./pow'),
 	sort = require('./sort'),
 	sub = require('./subtract'),
-	sum = require('./sum');
+	sum = require('./sum'),
+	union = require('./union');
 
 /* Data Structures */
 const Matrix = require('./Matrix'),
@@ -69,6 +70,7 @@ module.exports = {
 	avg,
 	inverse,
 	count,
+	union,
 	BinaryTree,
 	PriorityQueue,
 	Stack,
diff --git a/src/union/index.js b/src/union/index.js
new file mode 100644
index 0000000..bd05bbf
--- /dev/null
+++ b/src/union/index.js
@@ -0,0 +1,41 @@
+const validate = require('../validation/number-array');
+
+let union = (arr1, arr2) => {
+
+    validate(arr1, 'union');
+    validate(arr2, 'union');
+
+    arr1.sort();
+    arr2.sort();
+
+    let result = [], len = arr1.length + arr2.length, i = 0, j = 0;
+
+    while((i+j) < len){
+        if((i < arr1.length && j < arr2.length)){
+            if(arr1[i] < arr2[j]){
+                result.push(arr1[i]);
+                i++;
+            }
+            else if(arr1[i] > arr2[j]){
+                result.push(arr2[j]);
+                j++;
+            }
+            else{
+                result.push(arr1[i]);
+                i++;
+                j++;
+            }
+        }
+        else if(i == arr1.length){
+            result.push(arr2[j]);
+                j++;
+        }
+        else{
+            result.push(arr1[i]);
+            i++;
+        }
+    }
+    return result;
+};
+
+module.exports = union;
\ No newline at end of file


Test patch:
diff --git a/test/union-spec.js b/test/union-spec.js
new file mode 100644
index 0000000..51e91cf
--- /dev/null
+++ b/test/union-spec.js
@@ -0,0 +1,33 @@
+const assert = require('assert'),
+        union = require('../src/union');
+
+describe('[Function: union]', () => {
+    
+    it('should be a function', () => {
+		assert.strictEqual(typeof union, 'function');
+    });
+    
+    it('should throw an error when a non-array argument is passed', () => {
+		assert.throws(() => union('ssad', [1,2,34]), TypeError);
+	});
+
+    it('should throw an error when a non-array argument is passed', () => {
+		assert.throws(() => union([1,2,34], 'ssad'), TypeError);
+    });   
+
+    it('should return "[1, 2, 3, 4]" when "[1, 2, 3]" and "[2, 3, 4]" are passed', () => {
+		assert.deepEqual(union([2, 3, 4], [1, 2, 3]), [1, 2, 3, 4]);
+    });
+
+    it('should return "[1, 45, -10]" when "[1, 2, 3, 4]" and "[-10, 1, 2, 3, 4, 45]" are passed', () => {
+		assert.deepEqual(union([1, 45, -10], [1, 2, 3, 4]), [-10, 1, 2, 3, 4, 45]);
+    });
+
+    it('should return "[1, 2, 3, 4]" when "[1, 45, -10]" and "[-10, 1, 2, 3, 4, 45]" are passed', () => {
+        assert.deepEqual(union([1, 2, 3, 4], [1, 45, -10]), [-10, 1, 2, 3, 4, 45]);
+    });
+
+    it('should return "[1]" when "[1, 45, -10]" and "[-10, 1, 45]" are passed', () => {
+        assert.deepEqual(union([1, 45, -10], [1]), [-10, 1, 45]);
+    });
+});
\ No newline at end of file