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,232,848 Responses

  1. I like the valuable info you provide in your articles. I will bookmark your weblog and check again here frequently. I am quite certain I?ll learn plenty of new stuff right here! Good luck for the next!

  2. Enrique表示:

    Where Will AvonUK Be 1 Year From Today? avon; Enrique,

  3. pussy888表示:

    I read this paragraph fully concerning the comparison of most recent and previous technologies, it’s amazing article.

  4. The Three Greatest Moments In Motorcycle Accident Litigation History Lincoln city motorcycle accident lawyer

  5. I think this is one of the most important information for me.
    And i’m glad reading your article. But wanna remark on some general things,
    The web site style is perfect, the articles is really great : D.
    Good job, cheers

  6. What Is Upvc Sash Windows? History Of Upvc Sash Windows Upvc Windows Repair Near Me (https://Wayranks.Com/)

  7. TylerPhona表示:

    https://cenforce.pro/# cenforce for sale

  8. ElmerWes表示:

    sildenafil over the counter Buy Viagra online cheap sildenafil 50 mg price

  9. GeorgeSep表示:

    Vardenafil buy online: Vardenafil online prescription – Vardenafil price

  10. DavidOwelo表示:

    https://cialist.pro/# buy cialis pill

  11. JamesNow表示:

    Cenforce 100mg tablets for sale: buy cenforce – Buy Cenforce 100mg Online

  12. Jeffrey表示:

    The 3 Greatest Moments In Double Glazed Windows Repair History double glazed window
    repairs near me (Jeffrey)

  13. The Coffee Machine Awards: The Most Sexiest, Worst, And The Most Bizarre Things We’ve Seen portable coffee makers

  14. I really love your blog.. Excellent colors & theme. Did you make this site yourself?
    Please reply back as I’m planning to create my own blog and want to know where you got this from or just what
    the theme is named. Cheers!

  15. JamesNow表示:

    cenforce for sale: Purchase Cenforce Online – cheapest cenforce

  16. 5 Prada Bag Leçons From The Professionals Prada bag white

  17. Yvonne表示:

    Why Is Avon In UK So Popular? avon in uk (Yvonne)

  18. apk exness表示:

    Hello. Permit me to introduce the writer. His name is Tod.
    My day job is a production and planning officer and the salary
    has been really accomplishing. New Jersey has always been his home but he needs to handle because of his family member.
    Doing interior design is regarded as the whatever love a large
    amount.

  19. 10 Things You’ll Need To Know About Defra Approved Stove low emissions Stoves

  20. The Biggest Sources Of Inspiration Of Birth Defect Case mountain view birth defect lawsuit

  21. CharlesDog表示:

    https://kamagra.win/# buy Kamagra

  22. CharlesDog表示:

    https://kamagra.win/# п»їkamagra

  23. Who Is Responsible For An Multi Fuel Stove Budget? 12 Top Ways To Spend Your Money Inset multi Fuel stove

  24. The Most Hilarious Complaints We’ve Heard About Porsche Macan Key Programming porsche new key [dokuwiki.stream]

  25. Heya i am for the first time here. I found this board
    and I find It really useful & it helped me out a lot.
    I hope to give something back and help others like you aided me.

  26. CharlesDog表示:

    http://levitrav.store/# buy Levitra over the counter

  27. sgp188表示:

    Howdy great blog! Does running a blog similar to this require a massive amount work?
    I’ve very little knowledge of computer programming
    but I was hoping to start my own blog soon. Anyway, should you
    have any ideas or tips for new blog owners please share.
    I know this is off topic however I simply wanted to
    ask. Many thanks!

  28. window repair表示:

    Why You Should Concentrate On Improving Repair Upvc Window window repair

  29. JamesNow表示:

    Buy Tadalafil 10mg: buy cialis online – Generic Cialis without a doctor prescription

  30. Why Adhd Adults Treatment Is Still Relevant In 2023 adhd Treatment

發佈留言

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