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,244,067 Responses

  1. Top Earning App in Pakistan|Amazing Earning Opportunity in Pakistan|Innovative Earning Method in Pakistan|Earning App in Pakistan: Benefits|Profitable Earning App in Pakistan|Pakistan Earning Opportunities|Processing App in Pakistan: Proven Tool|New level of income in Pakistan|Interesting opportunities for earning in Pakistan|Pakistan: best app for processing
    money app in pakistan pakistan best earning app .

  2. AlbertDuM表示:

    lisinopril 1 mg tablet: buy Lisinopril online – cheapest Lisinopril

  3. When someone writes an article he/she retains the thought of a user in his/her mind that how a user can be aware of it.
    So that’s why this piece of writing is outstdanding. Thanks!

  4. вывод из запоя на дому краснодар круглосуточно http://wisdomtarot.tforums.org/viewtopic.php?f=16&t=11706 .

  5. DarrenUnjuh表示:

    http://iverfast.com/# ivermectin 500ml

  6. Markdob表示:

    Всем привет! Может кто знает, где найтиполезные блоги о недвижимости? Сейчас читаю – https://pellet-24.ru

  7. вывод из запоя краснодар на дому анонимно http://bija089.0pk.me/viewtopic.php?id=2554/ .

  8. Howdy, I think your web site might be having internet browser compatibility problems.
    Whenever I look at your website in Safari, it looks fine however, when opening in Internet Explorer, it has some overlapping
    issues. I just wanted to give you a quick heads up!
    Other than that, wonderful site!

  9. Markdob表示:

    Приветствую. Подскажите, где почитатьполезные статьи о недвижимости? Сейчас читаю – https://pellet-24.ru

  10. Boyd Copus表示:

    Converting Land to plantation

  11. Mable Gomez表示:

    Czy masz jakieś porady dotyczące wyboru materiałów do robótek ręcznych? To byłoby super przydatne! robótki ręczne na szydełku

  12. Nikefut表示:

    darkmarket link dark markets 2024 https://mydarknetmarketlinks.com/

  13. вывод из запоя анонимно/анонимный вывод из запоя http://familyportal.forumrom.com/viewtopic.php?id=28570#p70369 .

  14. DarrenUnjuh表示:

    https://iverfast.com/# minocycline weight gain

  15. each time i used to read smaller articles which also clear their motive, and that is also
    happening with this article which I am reading now.

  16. вывод из запоя цены на дому краснодар http://www.belbeer.borda.ru/?1-6-0-00000757-000-0-0-1730745253 .

  17. Best Earning App in Pakistan|Amazing Earning Opportunity in Pakistan|Earning Potential in Pakistan|Pakistan Money Processing Software|Profitable Earning App in Pakistan|Trustworthy Earning Platform in Pakistan|Pakistan Right Choice for Processing|Pakistan High Paying App for earnings|Pakistan: leader in earning|App that will make it easier to earn in Pakistan Pakistan
    earn money app pakistan pk earning app .

  18. We are a group of volunteers and starting a new scheme in our community.
    Your website provided us with valuable info to
    work on. You’ve done an impressive job and our entire community will be thankful to you.

  19. AlbertDuM表示:

    lisinopril1st: lisinopril 20 mg canadian pharmacy – Lisinopril 1st

發佈留言

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