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,523,626 Responses

  1. Dannyfut表示:

    tor markets links https://github.com/darknetmarketslinks/darknetmarketlinks – darkmarket drug markets dark web

  2. Rodneytus表示:

    dark market list https://github.com/darkwebmarketslinks/darkwebmarkets – dark web market darknet market links

  3. MarkWat表示:

    Привет. Может кто знает, где найтиразные статьи о кредитах? Пока нашел – https://vsekredity-tmn.ru

  4. MarkWat表示:

    Приветствую. Может кто знает, где почитатьразные блоги о займах? Пока нашел – 7sevenauto.ru

  5. Davidnof表示:

    deep web search https://github.com/darkwebwebsites/darkwebwebsites – deep web drug store darknet drugs

  6. Dannyfut表示:

    darknet links https://github.com/darknetmarketslinks/darknetmarketlinks – darknet drug store darkmarkets

  7. WillieRap表示:

    discount drug pharmacy: drug mart – no prescription required pharmacy

  8. A The Complete Guide To Buy Driving License A1 From Start To Finish kup prawo jazdy kat a

  9. Oh my goodness! Incredible article dude! Thanks, However I am going through problems with your RSS.
    I don’t know the reason why I cannot subscribe to it.
    Is there anybody having the same RSS issues? Anybody who knows
    the answer can you kindly respond? Thanks!!

  10. Lennyfut表示:

    bitcoin dark web https://github.com/darkmarketlinkp22jr/darkmarketlink – dark web drug marketplace

  11. 1win_fmOa表示:

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

  12. Porterfax表示:

    online pharmacy delivery usa online pharmacy delivery usa Best online pharmacy

  13. 1 vin_ksSi表示:

    официальный сайт 1win http://bbcc.com.kg .

  14. MichaelGeony表示:

    cheapest pharmacy to fill prescriptions without insurance https://familypharmacy.company/# family pharmacy

  15. 1win_phOa表示:

    1 win регистрация https://fabc.com.kg .

  16. salt trick表示:

    Good day I am so happy I found your site, I really found you by error, while I was looking on Google for something
    else, Anyhow I am here now and would just like to say thanks a lot for a marvelous post and a all round exciting blog (I also love the theme/design),
    I don’t have time to go through it all at the minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to
    read a great deal more, Please do keep up the great work.

  17. Viagra en ligne, ordre en ligne viagra, Conferencedesbatonniers. Meilleur ordonnance, 50mg en ligne escompte Canada acheter cialis pilule de. Pfizer cialis achat meilleur pharmacie cipla.Achat en ligne prescription du. Retrouvez les services Click Collect et Ordonnance en ligne de votre. Acheter viagra en ligne, viagra de achat Canada, Universal Leonardo. https://eurogeneriqueetfrance.com/imurek/ Acheter Imurek Pharmacie

  18. Porterfax表示:

    thecanadianpharmacy easy canadian pharm easy canadian pharm

  19. Rodneytus表示:

    darknet market list https://github.com/darkwebmarketslinks/darkwebmarkets – dark web link dark market url

  20. What Makes Online Casinos Have Become a Worldwide Trend

    Online casinos have reshaped the casino gaming market, delivering an unmatched level of convenience and range that traditional establishments fall short of. Over the past decade, a vast number of enthusiasts worldwide have adopted the pleasure of virtual casinos in light of its accessibility, engaging traits, and constantly growing selection of games.

    One of the main appeals of digital gambling sites is the unparalleled range of titles provided. Whether you prefer interacting with old-school one-armed bandits, immersing yourself in story-driven visual slot games, or mastering skills in classic casino games like Roulette, online platforms feature countless options. Several sites furthermore introduce live gaming streams, giving you the chance you to communicate with actual dealers and fellow gamblers, all while taking in the lifelike ambiance of a brick-and-mortar establishment right at home.

    If you’re unfamiliar with the world of digital casinos or hope to learn about proven options, why not sign up for our lively interactive platform? It’s a destination where enthusiasts exchange insights, guiding you to enhance your gaming journey. Join the community and start your journey now: 22bet recensioni

    Besides the wide selection, virtual gambling platforms shine accessibility.

  21. toto表示:

    You can definitely see your skills in the work you write.

    The arena hopes for even more passionate writers such as you who aren’t afraid to mention how they believe.
    Always follow your heart.

  22. JacobLoody表示:

    http://www.duc-sozvezdie.ru – Перейти на сайт производителя кухонь.

發佈留言

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