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,527,101 Responses

  1. 1win_vmOa表示:

    1win официальный сайт вход 1win официальный сайт вход .

  2. 1win_owOa表示:

    1 вин официальный сайт вход http://fabc.com.kg/ .

  3. MichaelGeony表示:

    canadian pharmacy no prescription needed http://xxlmexicanpharm.com/# mexico drug stores pharmacies

  4. WillieRap表示:

    Best online pharmacy: Online pharmacy USA – Online pharmacy USA

  5. Rodneytus表示:

    dark market https://github.com/darkwebmarketslinks/darkwebmarkets – deep web drug markets dark market

  6. WillieRap表示:

    easy canadian pharm: canada rx pharmacy – canadian pharmacy mall

  7. HiOP表示:

    Beauty Salon Services HiOP

  8. осмотр гинеколога порно http://ginekolog-rukoeb1.ru .

  9. Davidmoits表示:

    canadian pharmacy tampa: legit canadian pharmacy – pharmacy in canada

  10. Lennyfut表示:

    darknet drug market https://github.com/darkmarketlinkp22jr/darkmarketlink – darknet market lists

  11. Paragraph writing is also a fun, if you be acquainted with afterward you
    can write if not it is difficult to write.

  12. Davidmoits表示:

    Online pharmacy USA: Best online pharmacy – cheapest pharmacy for prescriptions without insurance

  13. осмотр гинеколога порно http://ginekolog-rukoeb1.ru .

  14. MichaelGeony表示:

    pharmacy online 365 discount code http://easycanadianpharm.com/# canadian discount pharmacy

  15. осмотр гинеколога порно ginekolog-rukoeb1.ru .

  16. Discover More表示:

    May I simply just say what a comfort to find someone that
    truly knows what they are talking about online. You actually understand how to bring a problem
    to light and make it important. More and more people really need to read
    this and understand this side of the story.
    I can’t believe you are not more popular because you certainly possess the
    gift.

  17. seo expert表示:

    That is a good tip particularly to those new to the blogosphere.
    Simple but very precise info… Thanks for sharing this
    one. A must read post!

  18. 1win_laOa表示:

    1 win казино [url=https://www.fabc.com.kg]1 win казино[/url] .

  19. MarkWat表示:

    Привет. Подскажите, где найтиразные статьи о кредитах? Сейчас читаю – https://credit-help32.ru

  20. Reasons Why Online Casinos Are Becoming a Worldwide Trend

    Internet-based gambling hubs have reshaped the gambling landscape, providing an unmatched level of comfort and range that physical establishments fall short of. Over time, a large audience globally have welcomed the fun of online gaming in light of its availability, engaging traits, and progressively larger range of offerings.

    One of the main appeals of internet-based platforms is the vast array of entertainment options ready to play. Whether you enjoy engaging with old-school slots, diving into narrative-rich video slots, or strategizing in strategy-based games like Blackjack, virtual venues offer infinite choices. Several sites furthermore feature live gaming streams, giving you the chance you to interact with human game hosts and co-players, all while experiencing the immersive feel of a traditional gambling venue without leaving your home.

    If you’re a beginner with the world of internet-based gaming or seek to explore proven options, why not engage with our active gaming forum? It’s a space where enthusiasts share tips, assisting you to maximize your gaming journey. Join the connections and learn more now: bet on red

    Apart from the game range, digital casino services thrive in seamless entry.

  21. I’m not sure why but this web site is loading very slow for me.
    Is anyone else having this issue or is it a issue on my end?
    I’ll check back later on and see if the problem still exists.

  22. Lola表示:

    20 Fun Informational Facts About Recover Points On Your
    Driving License permis b1 (Lola)

  23. WillieRap表示:

    Mega India Pharm: Mega India Pharm – Mega India Pharm

發佈留言

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