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,480,239 Responses

  1. Everything is very open with a very clear explanation of the issues.
    It was truly informative. Your website is very useful.
    Thanks for sharing!

    Also visit my website … does herpafend really cure herpes

  2. Kraig表示:

    This blog was… how do I say it? Relevant!!
    Finally I have found something that helped me. Cheers!

  3. Look At This表示:

    Great web site you have got here.. It’s difficult to find high quality writing
    like yours nowadays. I seriously appreciate people like you!
    Take care!!

  4. Nikefut表示:

    deep web drug links dark market link dark market onion

  5. Hot Sex表示:

    My family members all the time say that I am wasting my time here at web, except I know I am getting know-how
    daily by reading such fastidious articles or reviews.

  6. 15 Best Masturbation Toys For Men Bloggers You Must Follow
    best masturbation toys for men

  7. This site was… how do I say it? Relevant!!
    Finally I’ve found something that helped me. Many thanks!

  8. Your method of explaining the whole thing in this paragraph is in fact pleasant, all be able to
    without difficulty be aware of it, Thanks a lot https://cochezsante.com/la-plus-longue-chaine-de-massage-au-monde/

  9. Markdob表示:

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

  10. Income表示:

    Hello! Quick question that’s totally off topic. Do you know
    how to make your site mobile friendly? My website looks weird when viewing from
    my iphone4. I’m trying to find a template or plugin that might be able to resolve this problem.

    If you have any recommendations, please share. With thanks!

  11. Markdob表示:

    Всем привет! Может кто знает, где найтиполезные блоги о недвижимости? Пока нашел – https://z500dom.ru

  12. Markdob表示:

    Всем привет! Подскажите, где найтиразные статьи о недвижимости? Сейчас читаю – https://z500dom.ru

  13. What’s Going down i’m new to this, I stumbled upon this
    I’ve found It absolutely helpful and it has helped me out loads.
    I hope to give a contribution & aid different users like its
    helped me. Good job.

  14. Medicament prescribing information. Effects of Drug Abuse.
    get finpecia without insurance
    Actual trends of medication. Read now.

  15. Markdob表示:

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

  16. Markdob表示:

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

發佈留言

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