JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

805,123 Responses

  1. Williamjug表示:

    underground hackers black market dark net market list reddit

  2. Craigbup表示:

    underground hackers black market onion market url

  3. Hmm is anyone else experiencing problems with the images on this blog loading?
    I’m trying to figure out if its a problem on my end or if it’s the blog.

    Any suggestions would be greatly appreciated.

    Stop by my web blog :: BioLife Keto Gummies

  4. Beatriz表示:

    %%

    Also visit my website; double glaze windows – Beatriz

  5. Williamhar表示:

    bohemia market darknet vice city link

  6. Williamhar表示:

    what is the best darknet market dark markets croatia

  7. Williamhar表示:

    how to get on the dark web android dark markets philippines

  8. Ignacio表示:

    %%

    Feel free to visit my web site – composite doors high wycombe [Ignacio]

  9. Windows 10表示:

    Good day! Do you know if they make any plugins to safeguard against
    hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?

  10. %%

    Here is my web page – leather Flight jackets (http://Www.adsmos.com)

  11. Jaunita表示:

    %%

    my website sexdoll – Jaunita

  12. SteveDEW表示:

    зеркало мега mega darknet

  13. Fantastic goods from you, man. I have take into accout your stuff previous to
    and you’re simply too fantastic. I actually like what you’ve got right here, certainly like what you’re saying and the way in which by which you say it.
    You are making it enjoyable and you continue to take care of to keep it wise.
    I cant wait to learn much more from you. That is really a terrific website.

    My homepage :: playing online

  14. %%

    Feel free to visit my blog post; front door Replacement

  15. SteveDEW表示:

    ссылка на мегу зайти на мегу

  16. Gambler表示:

    %%

    Here is my web blog; Gambler

  17. Helⅼо, thee wholee tһin iis goinng nicey ere annd ofcourse evvery onee iis
    sharing fɑcts, tһat’s iin factt good, keep upp writing.

發佈回覆給「Craigbup」的留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。