Skip to content

Commit ca3800a

Browse files
committed
Refactor unique() using Array.forEach()
1 parent 22fb6f4 commit ca3800a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

libs/array_lib.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,21 @@ const extractDigits = function(number){
167167

168168
const unique = function(list){
169169
let result = [];
170-
for(let element of list){
170+
list.forEach(function(element){
171171
let isAlreadyPresent = result.includes(element);
172172
if(!isAlreadyPresent){
173173
result.push(element);
174174
}
175-
}
175+
});
176176
return result;
177177
}
178178

179179
// get Union set for two given sets
180180
const union = function(firstSet, secondSet){
181181
let unionSet = firstSet.slice();
182-
secondSet.forEach(function(element){ unionSet.push(element);});
182+
secondSet.forEach(function(element){
183+
unionSet.push(element);
184+
});
183185
return unique(unionSet);
184186
}
185187

0 commit comments

Comments
 (0)