Advertisement
Guest User

Untitled

a guest
Jan 31st, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     var matrix = [];
  3.     var seqLength = Number(arr[arr.length - 1]);
  4.     for (var i = 0; i < arr.length - 1; i++) {
  5.         var obj = arr[i].split(' ');
  6.         matrix.push(obj);
  7.     }
  8.     var merged = [].concat.apply([], matrix);
  9.     for (var i = 0; i < merged.length; i++) {
  10.         var repeats = 1;
  11.         for (var j = i + 1; j < i + seqLength; j++) {
  12.             if (merged[i] === merged[j]) {
  13.                 repeats++;
  14.             }
  15.         }
  16.         if (repeats === seqLength) {
  17.             for (var j = i; j < i + seqLength; j++) {
  18.                 merged[j] = 'removed';
  19.             }
  20.         }
  21.     }
  22.     for (var i = 0; i < matrix.length; i++) {
  23.         var row = merged.splice(0, matrix[i].length).filter(function (x) {
  24.             return x !== 'removed';
  25.         });
  26.         if (row.length) {
  27.             console.log(row.join(' '));
  28.         }
  29.         else {
  30.             console.log('(empty)');
  31.         }
  32.  
  33.     }
  34. }
  35. var arr = [ '3 3 3 2 5 9 9 9 9 1 2',
  36.     '1 1 1 1 1 2 5 8 1 1 7',
  37.     '7 7 1 2 3 5 7 4 4 1 2',
  38.     '2' ];
  39. arr = [ '1 2 3 3', '3 5 7 8', '3 2 2 1', '3' ];
  40. arr = [ '2 1 1 1', '1 1 1', '3 7 3 3 1', '2' ];
  41. arr = [ '1 2 3 3 2 1', '5 2 2 1 1 0', '3 3 1 3 3', '2' ];
  42. solve(arr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement