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

805,160 Responses

  1. Dan Mcgrath表示:

    you’re truly a good webmaster. The website loading pace
    is incredible. It seems that you are doing any unique trick.
    In addition, The contents are masterwork. you have performed a fantastic job in this subject!

    Here is my homepage; prefab steel buildings

  2. Riley表示:

    It is legal to play poker online, but be sure that you’re legally able to play.
    Poker sites online require that players are at least 18 years
    old to play. However, some sites may require a verification process before
    you can download their software.

    my homepage – bet – Riley,

  3. Penelope表示:

    %%

    Take a look at my page … top ten onlyfans (Penelope)

  4. Go here表示:

    A winning Powerbal ticket worth about $731 million was sold
    in Lonaconing, a modest town in Western Maryland’s Allegany
    County.

    Here is myy blog: Go here

  5. Williamhar表示:

    dark web drug marketplace dark web market list

  6. baccarat表示:

    Thanks for sharing your info. I truly appreciate your efforts and I
    will be waiting for your next post thank you once again.

    Look into my page baccarat

  7. cake carts表示:

    The information shared is of top quality which has to get appreciated at all levels. Well done…

  8. DarnellKar表示:

    the canadian drugstore prescription drugs without doctor approval

  9. An outstanding share! I’ve just forwarded this onto a co-worker who has
    been conducting a little research on this.
    And he in fact bought me lunch because I stumbled upon it for him…
    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending the time to talk about this topic
    here on your website.

  10. Skye Croll表示:

    Hi there, You’ve done an excellent job. I’ll definitely digg
    it and personally recommend to my friends. I’m sure they will
    be benefited from this site.

    Feel free to surf to my web-site; bookmarks

  11. bet表示:

    There are a variety of treatments for addiction to gambling.

    Some of them involve one-on one counseling, medication, and lifestyle changes.

    If you can’t stop yourself from engaging in this
    behaviour it can become an addiction.

    Here is my web-site bet

  12. Excellent post. I was checking constantly this blog and I am impressed!
    Extremely helpful information specially the last part 🙂 I care for such info a lot.
    I was seeking this particular info for a long time.
    Thank you and best of luck.

發佈回覆給「Go here」的留言 取消回覆

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