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,142,967 Responses

  1. 5 Killer Quora Answers To Designer Handbags Uk Sale designer handbags uk Sale

  2. Dangerous Drugs Law Firms Tools To Ease Your Daily Life
    Dangerous Drugs Law Firms Trick Every Individual Should
    Know dangerous drugs law firms

  3. 10 Kids Bunkbed Tips All Experts Recommend Best Kids Bunkbeds

  4. GeorgeSep表示:

    cenforce.pro: Cenforce 150 mg online – cenforce for sale

  5. The 3 Most Significant Disasters In Adult Adhd Symptoms History symptoms of adult female adhd

  6. ElmerWes表示:

    Sildenafil Citrate Tablets 100mg viagras.online Viagra generic over the counter

  7. Are 9kg Washing Machine For Sale The Most
    Effective Thing That Ever Was? cheapest 9kg washing machine –
    velo-Xachmas.com

  8. Volvo S60 Key表示:

    5 Motives Volvo Key Is Actually A Great Thing Volvo S60 Key

  9. JamesNow表示:

    generic sildenafil: Cheap Viagra 100mg – Cheap generic Viagra

  10. CharlesDog表示:

    https://kamagra.win/# super kamagra

  11. JamesNow表示:

    Viagra without a doctor prescription Canada: Buy Viagra online cheap – Viagra without a doctor prescription Canada

  12. Esteban表示:

    20 Freezers For Garages Websites That Are Taking The Internet By Storm quiet freezers (Esteban)

  13. huusk knives表示:

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my
    blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping
    maybe you would have some experience with
    something like this. Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

  14. 5 Killer Quora Answers To Designer Handbags Uk Sale designer handbags uk sale

  15. TylerPhona表示:

    https://levitrav.store/# Levitra online pharmacy

  16. Test: How Much Do You Know About Railroad Injuries Case?
    west dundee railroad injuries lawyer

  17. Wonderful goods from you, man. I’ve understand your stuff previous to and you are just extremely magnificent. I really like what you’ve acquired here, really like what you are saying and the way in which you say it. You make it entertaining and you still take care of to keep it wise. I cant wait to read far more from you. This is really a terrific website.

  18. Ingrid表示:

    The Most Important Reasons That People Succeed In The
    Personal Injury Lawyer Fee Industry dallas personal injury lawyer (Ingrid)

  19. ElmerWes表示:

    Buy Cialis online Generic Cialis without a doctor prescription Cheap Cialis

  20. CharlesDog表示:

    https://cenforce.pro/# buy cenforce

  21. Cheapest Beko Washing Machine’s History History Of Cheapest Beko Washing Machines Beko Washing Machine

  22. DavidOwelo表示:

    https://cenforce.pro/# order cenforce

  23. The Most Profound Problems In Injury Litigation Mount ephraim Injury attorney

  24. 20 Reasons To Believe Car Key Replacement Near Me Will
    Never Be Forgotten emergency car key replacement near me

  25. ElmerWes表示:

    п»їLevitra price Levitra generic price Cheap Levitra online

  26. The 12 Types Of Twitter Uk Online Phone Shopping Sites People You Follow On Twitter
    best Online shopping sites in uk for clothes

  27. ElmerWes表示:

    Buy Tadalafil 10mg buy cialis overseas п»їcialis generic

  28. ElmerWes表示:

    Kamagra Oral Jelly kamagra oral jelly super kamagra

發佈留言

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