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...

987,733 Responses

  1. ccliveapp表示:

    What’s up to every one, it’s genuinely a pleasant for
    me to go to see this website, it includes valuable Information.

  2. See What Online Charity Shop Uk Clothes Tricks The Celebs Are Using Online charity shop uk Clothes

  3. SEO_tiMa表示:

    поисковое продвижение сайтов в москве https://www.prodvizhenie-sajtov-v-moskve115.ru .

  4. lawyers表示:

    Ten Things You Should Never Share On Twitter lawyers

  5. Five Killer Quora Answers To Best Online Shopping Websites Uk best online shopping websites uk

  6. The 10 Most Terrifying Things About 18 Wheeler Accident Lawyers 18 Wheeler Accident Lawyers

  7. nolvadex otc表示:

    Drug prescribing information. What side effects?
    nolvadex otc
    Best what you want to know about drugs. Get now.

  8. Pills prescribing information. What side effects?
    where can i get generic prednisolone without a prescription
    Some what you want to know about medicines. Read here.

  9. I’ll right away take hold of your rss as I can not in finding
    your e-mail subscription link or newsletter service.
    Do you have any? Please let me realize so that I
    could subscribe. Thanks.

  10. You’ll Be Unable To Guess Generalised Anxiety Disorder’s Secrets Generalised anxiety Disorder

  11. Everything You Need To Be Aware Of Ecommerce SEO Agency search Engine Marketing agency

  12. What’s The Current Job Market For Mixed Anxiety Depressive Disorder
    Professionals Like? mixed anxiety depressive disorder

  13. Hey There. I found your blog using msn. This is a very well written article.
    I’ll make sure to bookmark it and return to read more of your useful information. Thanks
    for the post. I will definitely return.

  14. Andrewtax表示:

    Kylian Mbappe Lotten https://kylianmbappe.prostoprosport-br.com Futebolista frances, atacante do Paris Saint-Germain e capitao da selecao francesa equipe . Em 1? de julho de 2024, ele se tornara jogador do clube espanhol Real Madrid.

  15. Medicine information for patients. Long-Term Effects.
    how to buy generic ventolin without insurance
    Some trends of drug. Get information here.

  16. Eddiewhody表示:

    Carlos Henrique Casimiro https://carloscasemiro.prostoprosport-br.com Futebolista brasileiro, volante do clube ingles Manchester United e capitao do Selecao Brasileira. Pentacampeao da Liga dos Campeoes da UEFA, campeao mundial e sul-americano pela selecao juvenil brasileira.

  17. Louella表示:

    I like the helpful information you provide in your articles.
    I’ll bookmark your weblog and check again here frequently.
    I’m quite sure I will learn a lot of new stuff right here!

    Good luck for the next!

  18. Guide To Slot Mechanics: The Intermediate Guide For Slot
    Mechanics Slot Mechanics

  19. What’s The Current Job Market For Velvet Sectional Sofa Professionals Like?
    velvet Sectional sofa

  20. r7表示:

    Максимальный возможный выигрыш с кэш-бонуса составляет х10 от его суммы.

  21. Medicines information. Effects of Drug Abuse.
    zithromax tablets price
    Some news about medicines. Get here.

  22. 4Mph Scooter表示:

    There Are A Few Reasons That People Can Succeed With The 4mph Mobility Scooter Industry 4Mph Scooter

  23. Having read this I thought it was really enlightening.
    I appreciate you spending some time and energy to put this short article
    together. I once again find myself personally spending a significant amount of time both reading and posting comments.
    But so what, it was still worth it!

  24. 8 Tips To Improve Your Bunk With Double Bed Game white double Bunk Bed

發佈留言

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