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...

909,840 Responses

  1. Highly descriptive blog, I liked that a lot. Will there be a part 2?

    my site Derma ProX Reviews

  2. poker表示:

    There are many different rules and regulations that govern online gambling.
    In the US, for example it is not possible to establish
    an online gambling site in the country you reside in however you can locate it elsewhere.

    Feel free to surf to my webpage: poker

  3. blackcap表示:

    Simply wish to say your article is as amazing.

    The clarity in your post is just cool and i could assume you are an expert
    on this subject. Fine with your permission allow me to grab your feed to
    keep updated with forthcoming post. Thanks a million and please continue the gratifying
    work.

  4. The internet offers a variety of advantages for both bookmakers and the bettors.

    In addition to allowing bettors to place bets on their preferred sports Online (http://www.whydesign.co.Kr) betting sites
    allow them to earn money. They provide a wide
    range of deposit options.

  5. JuliusKaw表示:

    darknet market links dark web sites

  6. Top Only fan表示:

    %%

    Look at my web page :: Top Only fan

  7. Craigbup表示:

    #file_links[“C:\xrukeys\kw-darknet.txt,1,S – #file_links[“C:\xrukeys\kw-darknet.txt,1,S –

  8. What’s up to all, as I am actually eager of reading this blog’s post to be updated
    daily. It includes pleasant information.

  9. Craigbup表示:

    #file_links[“C:\xrukeys\kw-darknet.txt,1,S – #file_links[“C:\xrukeys\kw-darknet.txt,1,S –

  10. JuliusKaw表示:

    best darknet markets darkmarket

  11. Ayak yalayan’s Tweets. Ayak yalayan Retweeted. Pee Community® Hep erkekler
    mi saksoyla uyandıracak. Kız erkek arkadaşını ağzına boşalarak
    uyandırıyor. The following media includes potentially sensitive
    content. Change settings. View.

發佈留言

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