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,122,732 Responses

  1. Профессиональный сервисный центр по ремонту планетов в том числе Apple iPad.
    Мы предлагаем: ремонт ipad в москве на дому
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  2. 15 Things You Didn’t Know About Smart Car Keys Smart Keys Cars

  3. program表示:

    The 9 Things Your Parents Taught You About Car Key Programmer program

  4. I loved as much as you will receive carried out right here.
    The sketch is tasteful, your authored subject matter
    stylish. nonetheless, you command get got an nervousness over that
    you wish be delivering the following. unwell unquestionably come further formerly
    again since exactly the same nearly a lot often inside case
    you shield this increase.

  5. Darrelherse表示:

    slot casino siteleri: slot siteleri – yasal slot siteleri

  6. KevinSounc表示:

    https://sweetbonanza.network/# sweet bonanza mostbet

  7. KevinSounc表示:

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

  8. Hello and welcome. My name is Santo Cifuentes. Maryland
    is your biggest I love most. Fishing is the thing she loves most of all.
    She works as a payroll clerk but she plans on changing in which.

  9. Have you ever considered about adding a little bit more than just your articles?

    I mean, what you say is valuable and all. But just
    imagine if you added some great photos or video clips
    to give your posts more, “pop”! Your content is excellent but with
    images and clips, this site could undeniably be one of the very best in its field.

    Good blog!

    Here is my web site … look at this web-site

  10. See What Best Robot Vacuum Mop For Pet Hair Tricks The Celebs Are Using
    best robot vacuum mop for pet hair

  11. The Top Kia Ceed Key Fob Replacement Gurus Can Do Three Things
    2013 Kia optima key replacement

  12. Профессиональный сервисный центр по ремонту планетов в том числе Apple iPad.
    Мы предлагаем: ремонт планшетов на дому
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  13. How Much Can Mini Cooper Key Replacement Experts Earn? Mini cooper Key not working

  14. Fastidious response in return of this question with
    solid arguments and explaining all regarding that.

  15. I’m amazed, I have to admit. Seldom do I encounter a blog that’s equally educative and engaging, and without
    a doubt, you have hit the nail on the head. The issue is something that not enough men and
    women are speaking intelligently about. Now i’m very happy I found this during my hunt for
    something relating to this.

  16. Hi! I just wanted to ask if you ever have any problems with hackers?
    My last blog (wordpress) was hacked and I ended up losing a
    few months of hard work due to no back up. Do
    you have any methods to stop hackers?

  17. Five Killer Quora Answers On L Shaped Sleeper Sofa l shaped Sleeper Sofa

  18. Are Adjustable Clubs Inside Your Future? 수성구오피 (https://sv.rilpedia.org)

  19. Профессиональный сервисный центр по ремонту планетов в том числе Apple iPad.
    Мы предлагаем: сервис ipad москва
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  20. Darrelherse表示:

    yeni slot siteleri: en cok kazandiran slot siteleri – deneme bonusu veren siteler

  21. What’s up, just wanted to tell you, I loved this article.
    It was helpful. Keep on posting!

  22. Wonderful work! That is the kind of information that
    are supposed to be shared across the net. Shame on the search engines for
    now not positioning this put up upper! Come on over and
    talk over with my site . Thanks =)

  23. Darrelherse表示:

    casino slot siteleri: deneme bonusu veren slot siteleri – slot siteleri bonus veren

  24. Remarkable! Its really remarkable piece of writing, I have got much
    clear idea about from this paragraph.

  25. Darrelherse表示:

    slot oyun siteleri: slot siteleri 2024 – guvenilir slot siteleri

發佈留言

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