2017-12-02 151 views
2

Basiclly我想要做的是將數組輸入的值排序爲像uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])這樣的排序二維數組,如[[1, 1, 1], [2, 2, 2], [4], [3], [5]]陣列不一定是編號/值順序。下面的代碼是我的嘗試:排序數組(二維數組)需要幫助

function uniteUnique(arr) { 
    let times = 0; 
    var unique = [[]]; 
    for (var i = 0; i < Array.prototype.slice.call(arguments).length; i++) { 
     times = 0; 
     for (var j = 0; j < arguments[i].length; j++) { 
      for (var h = 0; h < unique.length; h++) { 
       var pushArray = [] 
       if(unique[h][0] === arguments[i][j]) { 
        unique[h].push(arguments[i][j]) 
        arguments[i].splice(j) 
       } 
       else { 
        unique.push([arguments[i][j]]) 
       } 
      } 
     } 
    } 
    return unique 
} 

uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]); 

- >https://repl.it/@John_Nicole/unique

我只有一個參數對所有的輸入數組


我有一個unique陣列1的空白數組。它是一個二維數組。

然後我通過,例如,[1, 3, 2]的值,並檢查unique中是否有第一個值作爲我的編號(arguments[i][j])。

如果爲true,我推動數字arguments[i][j],然後刪除原始數組中的數字splice()

如果爲false,我將新陣列插入到unique中,並帶有這個無法識別的值。

變量

  • h的快速概述:這是unique數組,我要與比較。它可能是[2, 2, 2]。這是從輸入數組。例如,[1, 3, 2]
  • j:這個號碼本身,它的夥伴i。例如,arguments[i][j] = [2, 1] - >2
  • arguments抓住所有,在這種情況下,3輸入數組。
  • Times:這只是意味着0.沒有用過這麼多。

輸入可能包括在自己的二維數組一樣[1, 3, 2], [1, [5]], [2, [4]]

這是一個freeCodeCamp挑戰的一部分 - >https://www.freecodecamp.org/challenges/sorted-union


我的問題是,爲什麼我的輸出:

[ [], 
    [ 1, 1 ], 
    [ 5, 5 ], 
    [ 5 ], 
    [ undefined, undefined ], 
    [ 2, 2 ], 
    [ 2 ], 
    [ 2 ], 
    [ 2 ], 
    [ 2 ], 
    [ undefined, undefined ], 
    [ undefined, undefined ], 
    [ undefined, undefined ], 
    [ undefined, undefined ] ] 

? 當需要的輸出是:通緝輸出是[1, 1, 1], [2, 2, 2], [4], [3], [5]

我做錯了什麼?

我得到了多個2的數組,例如([[ 2 ],[ 2 ],[ 2 ],[ 2 ]]),即使我想將所有2放入一個數組中?


+0

所需結果與給定的代碼挑戰不同。你喜歡哪一個? –

+0

我只是試圖按數值對它們進行排序,所以我知道它們有多少。我將把它們全部加起來,並決定它們有多少個起始數組,以查看它們是唯一值,只存在於一個數組中,還是存在於所有數組中。需求中的一些示例顯示了這一點。 – Leed

+0

我剛剛編輯我的帖子,使其更容易混淆 – Leed

回答

1

你可以使用一個哈希表,並檢查哈希鍵是否存在,如果沒有,然後拿一個空數組作爲哈希值,並將它推到結果集。

哈希表是一個對象(這裏沒有原型),它將值作爲鍵和數組作爲值。散列表的末尾保存數組中的所有值,如果找到新值,則將其插入結果集中。

{ 
    1: [1, 1, 1], 
    2: [2, 2, 2], 
    3: [3], 
    4: [4], 
    5: [5] 
} 

function uniteUnique() { 
 
    var result = [], 
 
     hash = Object.create(null); 
 
     
 
    Array.prototype.forEach.call(arguments, function (a) { 
 
     a.forEach(function (b) { 
 
      if (!(b in hash)) { 
 
       hash[b] = []; 
 
       result.push(hash[b]); 
 
      } 
 
      hash[b].push(b); 
 
     }); 
 
    }); 
 
    return result; 
 
} 
 

 
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));
.as-console-wrapper { max-height: 100% !important; top: 0; }


一些註釋代碼(未使用的變量被刪除):

function uniteUnique() { 
 
    var unique = [],         // move all declarations to top 
 
     i, j, h, 
 
     pushed; 
 

 
    // array like objects have a length property 
 
    for (i = 0; i < arguments.length; i++) { 
 
     for (j = 0; j < arguments[i].length; j++) { 
 
      pushed = false; 
 
      for (h = 0; h < unique.length; h++) { 
 
       if (unique[h][0] === arguments[i][j]) { 
 
        // take the element, do not use splice, because with splicing 
 
        // the array becomes shorter and the index is updated in the 
 
        // next loop and is pointing to the element with the wrong index, 
 
        // because you get the element after next 
 
        // it is better not to mutate a variable, if it works without 
 
        // in this case, you iterate and visit each element only once 
 
        unique[h].push(arguments[i][j]); 
 
        pushed = true;      // a value is found 
 
        break;        // exit this loop 
 
       }          // prevent more looping 
 
      } 
 
      if (!pushed) {        // if not found 
 
       unique.push([arguments[i][j]]);   // push the value 
 
      } 
 
     } 
 
    } 
 
    return unique; 
 
} 
 

 
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

輸入不是二維數組。 '''uniteUnique([1,3,2],[5,2,1,4],[2,1]);''' – Leed

+0

好吧,我改變了。 –

+0

哈希表如何工作?我的原始代碼有多遠?我仍然對這是如何工作感到困惑,我不想使用我不明白的代碼。 – Leed