-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdedupe-array.js
152 lines (110 loc) · 5.41 KB
/
dedupe-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
# Problem
- remove duplicates from array
# Assumptions
- solutions are written in vanilla es6 javascript
- we only checking for equality of strings and numbers
- feel free to run in node or in dev tools of chrome/firefox/safari (make sure that es6 related flags are turned on)
- simple set of unit tests provided
# Descriptions
There are multiple ways to de-dupe array, following few are documented here:
*/
/* DATA SET */
const SAMPLE_INPUT_DATA = [1, 2, 'COOL', 'FOR', 'many', 'AWESOME', 'cool', 'COOL', 2, 3];
const SAMPLE_OUTPUT_DATA = [1, 2, 'COOL', 'FOR', 'many', 'AWESOME', 'cool', 3];
/* IMPLEMENTATIONS */
/* Implementation using Set */
function removeDuplicatesInArrayWithSet (array) {
/* INFO:
Removing duplicates with set (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) and spread operator (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)
Pros:
Cons:
*/
const deDupedArray = new Set(array);
return [ ...deDupedArray ];
/* Bonus Points -> Could be simplified into one liner */
// return [ ...new Set(array) ];
}
/* Implementation using filter */
function removeDuplicatesInArrayWithFilter (array) {
/* INFO:
Removing duplicates with filter (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) and indexOf (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
Pros:
Cons:
*/
return array.filter((item, index) => {
return array.indexOf(item) === index;
})
}
/* Implementation using reduce */
function removeDuplicatesInArrayWithReduce (array) {
/* INFO:
Removing duplicates with reduce (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) and includes (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)
Pros:
Cons:
*/
return array.reduce((accumulator, item) => {
return accumulator.includes(item) ? accumulator : [...accumulator, item];
}, []);
}
/* Implementation using loop */
function removeDuplicatesInArrayWithLoop (array) {
/* INFO:
Removing duplicates with loop
Pros:
Cons:
*/
const deDupedArray = [];
array.forEach((item, index) => {
if (deDupedArray.indexOf(item) === -1) {
deDupedArray.push(item);
}
});
return deDupedArray;
}
/* TESTS */
function it (itShouldMessage, callback) {
console.log('\n')
console.log(`IT ${itShouldMessage}`);
callback();
}
function assertionToBeTrue (assertionMessage, calculatedValue, expectedResult) {
const isTrue = calculatedValue === expectedResult;
const passedOrFailedMessage = (isTrue ? 'PASSED' : 'FAILED');
console.log(`${passedOrFailedMessage} - ${assertionMessage}`);
if (!isTrue) {
console.log('Received: ', calculatedValue);
console.log('Expected: ', expectedResult);
}
}
(function runTests () {
it('should filter out exact duplicates when passing array to removeDuplicatesInArrayWithSet', () => {
const deDupedArray = removeDuplicatesInArrayWithSet(SAMPLE_INPUT_DATA);
assertionToBeTrue(`Should have length of ${SAMPLE_OUTPUT_DATA.length}`, deDupedArray.length, SAMPLE_OUTPUT_DATA.length);
assertionToBeTrue('Should have matching values at index 0', deDupedArray[0], SAMPLE_OUTPUT_DATA[0]);
assertionToBeTrue('Should have matching values at index 4', deDupedArray[4], SAMPLE_OUTPUT_DATA[4]);
assertionToBeTrue('Should have matching values at index 7', deDupedArray[7], SAMPLE_OUTPUT_DATA[7]);
});
it('should filter out exact duplicates when passing array to removeDuplicatesInArrayWithFilter', () => {
const deDupedArray = removeDuplicatesInArrayWithFilter(SAMPLE_INPUT_DATA);
assertionToBeTrue(`Should have length of ${SAMPLE_OUTPUT_DATA.length}`, deDupedArray.length, SAMPLE_OUTPUT_DATA.length);
assertionToBeTrue('Should have matching values at index 0', deDupedArray[0], SAMPLE_OUTPUT_DATA[0]);
assertionToBeTrue('Should have matching values at index 4', deDupedArray[4], SAMPLE_OUTPUT_DATA[4]);
assertionToBeTrue('Should have matching values at index 7', deDupedArray[7], SAMPLE_OUTPUT_DATA[7]);
});
it('should filter out exact duplicates when passing array to removeDuplicatesInArrayWithReduce', () => {
const deDupedArray = removeDuplicatesInArrayWithReduce(SAMPLE_INPUT_DATA);
assertionToBeTrue(`Should have length of ${SAMPLE_OUTPUT_DATA.length}`, deDupedArray.length, SAMPLE_OUTPUT_DATA.length);
assertionToBeTrue('Should have matching values at index 0', deDupedArray[0], SAMPLE_OUTPUT_DATA[0]);
assertionToBeTrue('Should have matching values at index 4', deDupedArray[4], SAMPLE_OUTPUT_DATA[4]);
assertionToBeTrue('Should have matching values at index 7', deDupedArray[7], SAMPLE_OUTPUT_DATA[7]);
});
it('should filter out exact duplicates when passing array to removeDuplicatesInArrayWithLoop', () => {
const deDupedArray = removeDuplicatesInArrayWithLoop(SAMPLE_INPUT_DATA);
assertionToBeTrue(`Should have length of ${SAMPLE_OUTPUT_DATA.length}`, deDupedArray.length, SAMPLE_OUTPUT_DATA.length);
assertionToBeTrue('Should have matching values at index 0', deDupedArray[0], SAMPLE_OUTPUT_DATA[0]);
assertionToBeTrue('Should have matching values at index 4', deDupedArray[4], SAMPLE_OUTPUT_DATA[4]);
assertionToBeTrue('Should have matching values at index 7', deDupedArray[7], SAMPLE_OUTPUT_DATA[7]);
});
console.log('\n')
})();