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

1,524,012 Responses

  1. mostbet_hkpa表示:

    mostbet com официальный сайт http://www.gtrtt.com.kg/ .

  2. Fusion bars表示:

    Wow, awesome blog layout! How lengthy have you ever been running
    a blog for? you made blogging look easy. The total look of your website is excellent,
    let alone the content!

  3. Rodneytus表示:

    free dark web https://github.com/darkwebmarketslinks/darkwebmarkets – dark web websites darknet drugs

  4. WillieRap表示:

    drug mart: discount drug pharmacy – drugmart

  5. Porterfax表示:

    online pharmacy delivery usa online pharmacy delivery usa Online pharmacy USA

  6. JacobLoody表示:

    ded-moros.ru/ – Перейти на сайт производителя кухонь.

  7. Tegan表示:

    The Reason Why You’re Not Succeeding At Buy Driving License
    Online Köpa A2 Körkort Online [Tegan]

  8. A testnet faucet provides web3 developers with free tokens for deploying, testing, and optimizing smart contracts
    on test blockchains such as Sepolia, Goerli, and Mumbai.

    Because smart contracts on public, mainnet blockchains like Ethereum and
    Polygon require gas fees to run smart contracts, testnets provide blockchain developers with a network that mirrors production blockchain environments without requiring gas fees that cost real money.

    Get Testnet Tokens!

  9. mostbet_uxpa表示:

    мостбет вход на сегодня http://www.gtrtt.com.kg/ .

  10. I think this is one of the so much significant info for me.

    And i am glad reading your article. But should statement on some
    normal things, The website style is perfect, the articles is truly excellent : D.
    Just right task, cheers

  11. mostbet_qypa表示:

    mostbet регистрация com https://www.gtrtt.com.kg .

  12. Rodneytus表示:

    dark website https://github.com/darkwebmarketslinks/darkwebmarkets – tor markets links bitcoin dark web

  13. Thanks for the good writeup. It if truth be told
    was a leisure account it. Glance complicated to far added agreeable from you!
    However, how could we be in contact?

  14. What Is Psychiatric Assessment? History Of Psychiatric Assessment what is psychiatric assessment

  15. Davidmoits表示:

    Best online pharmacy: Online pharmacy USA – family pharmacy

  16. A Three Day Itinerary In Hanoi – Northern Vietnam 하노이밤문화

  17. WillieRap表示:

    Cheapest online pharmacy: Best online pharmacy – no prescription needed pharmacy

  18. 1win_rgOa表示:

    1win партнерка вход http://www.fabc.com.kg .

  19. Dannyfut表示:

    deep web search https://github.com/darknetmarketslinks/darknetmarketlinks – dark web market dark markets 2025

  20. mostbet_erpa表示:

    мостбет компьютерная версия http://www.gtrtt.com.kg/ .

發佈留言

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