1

I have printed result from an array which looks like following;

A250.1 - 0h 34 m
A450.1 - 0h 34 m
A240.2 - 1h 34 m
A510.2 - 1h 34 m
A440.2 - 1h 34 m
A520.7 - 1h 34 m
A350.1 - 3h 19 m
A450.3 - 3h 34 m
A340.1 - 3h 34 m
A250.1 - 3h 34 m
A320.6 - 3h 34 m
A210.2 - 4h 19 m
A240.4 - 5h 34 m
A240.2 - 5h 34 m

So we have here A250.1 - 0h 34 m and A250.1 - 3h 34 m. What is the most efficient way to filter out the second one with the same name (A250.1 - 3h 34 m) from the first one (A250.1 - 0h 34 m)? (The other A250.1 will pop back to visible, when the first ones time expires.

TO CLARIFY; When the time expires it no longer shows the element in the filtered array.

I would like the result look like following;

A250.1 - 0h 34 m
A450.1 - 0h 34 m
A240.2 - 1h 34 m
A510.2 - 1h 34 m
A440.2 - 1h 34 m
A520.7 - 1h 34 m
A350.1 - 3h 19 m
A450.3 - 3h 34 m
A340.1 - 3h 34 m
A320.6 - 3h 34 m
A210.2 - 4h 19 m
A240.4 - 5h 34 m
A240.2 - 5h 34 m

// REMOVED (2nd) A250.1 - 3h 34 m 

Some further information about this can bee looked at here; JavaScript - Comparing two arrays with same strings

Community
  • 1
  • 1
Cod3r-b0ss
  • 205
  • 1
  • 10

1 Answers1

0
 var input_array = [
     "A250.1 - 0h 34 m",
     "A450.1 - 0h 34 m",
     "A240.2 - 1h 34 m",
     "A510.2 - 1h 34 m",
     "A440.2 - 1h 34 m",
     "A520.7 - 1h 34 m",
     "A350.1 - 3h 19 m",
     "A450.3 - 3h 34 m",
     "A340.1 - 3h 34 m",
     "A250.1 - 3h 34 m",
     "A320.6 - 3h 34 m",
     "A210.2 - 4h 19 m",
     "A240.4 - 5h 34 m",
     "A240.2 - 5h 34 m"
 ];
 var output_array = []
 var tmp_array = []
 for (var i = 0; i < input_array.length - 1; i++) {
     var result = input_array[i].split(' - ');
     if (tmp_array.indexOf(result[0]) == -1) {
         tmp_array.push(result[0]);
         output_array.push(result[0].concat(' - ' + result[1]))
     }
 }
 console.log(output_array);

Try this code,it will work. Check on https://codepad.remoteinterview.io/LLPNUEZDFT

Mani Selva
  • 54
  • 4