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,238,390 Responses

  1. ayrıca /bunun dışında, mostbet casino giriş
    Mostbet, mevcut size fırsat indirme yazılım most bet’ten sadakat
    sunar.

  2. These are genuinely enormous ideas in concerning blogging.
    You have touched some fastidious things here. Any way keep up wrinting.

  3. Расстановки. Алгоритм и варианты. https://rasstanovkiural.ru

  4. The moment is perfect, but you will get confused and play https://wtpglobalau.com/ with a true chorus. If necessary, then include it in the program.

  5. Guide To Train Derailment Attorney: The Intermediate Guide For Train Derailment Attorney train derailment attorney

  6. 20 Misconceptions About ADD And Treatment: Busted
    adhd Treatment near me

  7. That is very interesting, You are a very professional blogger.
    I have joined your feed and look ahead to
    in the hunt for more of your wonderful post. Additionally,
    I have shared your site in my social networks

  8. whoah this weblog is fantastic i like reading your articles.
    Keep up the good work! You realize, lots of persons are hunting round for this info, you could aid them greatly.

  9. Darrelherse表示:

    en yeni slot siteleri: slot oyunlar? siteleri – deneme bonusu veren slot siteleri

  10. Meds information for patients. Long-Term Effects.
    where to buy nolvadex no prescription
    All information about drug. Read information now.

  11. zwoltek01表示:

    Right here is the perfect web site for anybody who would like to understand this topic.
    You realize so much its almost hard to argue with you (not that I
    really will need to…HaHa). You definitely put a brand new spin on a subject that’s been discussed
    for ages. Wonderful stuff, just excellent!

    My website: zwoltek01

  12. bokep terbaru表示:

    Hello there! Would you mind if I share your blog with my twitter
    group? There’s a lot of people that I think would really enjoy your
    content. Please let me know. Many thanks

  13. The Best Adult Adhd Assessment Near Me Methods To Rewrite Your Life Adhd Assessment Adults Uk [http://Www.Harimint.Com]

  14. Hi there, I found your web site by means of Google whilst looking for a similar topic,
    your web site got here up, it appears to be like good. I’ve bookmarked it in my
    google bookmarks.
    Hello there, just become alert to your blog via Google, and
    located that it is truly informative. I am gonna be careful
    for brussels. I’ll be grateful in the event you
    continue this in future. Many people will probably
    be benefited out of your writing. Cheers!

  15. After exploring a handful of the blog posts on your web page, I honestly like your technique of
    blogging. I saved it to my bookmark site list and
    will be checking back soon. Take a look at
    my web site as well and let me know what you think.

  16. gay_xipr表示:

    Онлайн-букмекер для геев | Gay 1xbet: уникальные возможности для LGBTQ+ | 1xbet: безопасные ставки для геев | Секреты успешных ставок на спорт для ЛГБТ на 1xbet | Gay ставки на 1xbet: всё, что вам нужно знать
    gay porm in 1xbet gay porm in 1xbet .

  17. Darrelherse表示:

    slot siteleri bonus veren: slot kumar siteleri – slot siteleri bonus veren

  18. bokep terbaru表示:

    Great post. I will be going through many of these
    issues as well..

  19. KevinSounc表示:

    https://slotsiteleri.bid/# en iyi slot siteleri 2024

  20. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza bahis

  21. gay_pkpr表示:

    Лучшие спортивные ставки на гей-тематику | 1xbet для геев: новые горизонты | Gay-friendly 1xbet: ставки с комфортом | Как делать ставки на 1xbet, если вы гей | 1xbet: надёжные ставки для геев
    gsy porn in 1xbet gsy porn in 1xbet .

  22. Howdy! I could have sworn I’ve visited this website before but after looking at some of the posts I
    realized it’s new to me. Anyhow, I’m definitely pleased I discovered it
    and I’ll be bookmarking it and checking back regularly!

  23. I pay a quick visit everyday a few websites and information sites to read posts,
    however this webpage offers feature based posts.

  24. 20 Trailblazers Are Leading The Way In Getting Diagnosed With
    ADHD adhd adults diagnosis

  25. Darrelherse表示:

    slot oyun siteleri: slot siteleri 2024 – en cok kazandiran slot siteleri

  26. Every weekend i used to go to see this web page, because i wish for enjoyment, for the reason that this this web site
    conations actually pleasant funny material too.

  27. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza 100 tl

  28. The Three Greatest Moments In Adhd Assessment In Adults History how Do adults get Assessed for adhd

發佈留言

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