File tree 1 file changed +8
-4
lines changed
1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -238,7 +238,11 @@ const wordsUnique = [
238
238
'crab' ,
239
239
'crab' ,
240
240
'bee' ,
241
- 'bee'
241
+ 'foo' ,
242
+ 'bee' ,
243
+ 'crab' ,
244
+ 'crab' ,
245
+ 'foo'
242
246
] ;
243
247
244
248
function uniquifyArray ( array ) {
@@ -247,17 +251,17 @@ function uniquifyArray(array) {
247
251
return null ;
248
252
}
249
253
else {
250
- // loop over words in array
251
- for ( let i = 0 ; i < array . length ; i ++ ) {
254
+ // loop over words in array (looping backwards)
255
+ for ( let i = array . length - 1 ; i >= 0 ; i -- ) {
252
256
let currentWord = array [ i ] ;
253
- let currentWordIndex = i ;
254
257
255
258
// compare current word to all following words in array
256
259
for ( let j = i + 1 ; j < array . length ; j ++ ) {
257
260
// remove duplicates
258
261
if ( currentWord === array [ j ] ) {
259
262
array . splice ( j , 1 ) ;
260
263
}
264
+ else { }
261
265
}
262
266
}
263
267
return array ;
You can’t perform that action at this time.
0 commit comments