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,141,855 Responses

  1. memek cantik表示:

    Hi to every one, the contents present at this website are genuinely remarkable for people knowledge, well, keep up the nice work fellows.

  2. Vimeo.Com表示:

    Online Shopping Uk Electronics Tips From The Top In The Industry Vimeo.Com

  3. Five Killer Quora Answers To Slot Gaming Experience slot Gaming Experience

  4. 16 Must-Follow Facebook Pages To Upvc Window Repairs-Related Businesses upvc window repairs near me

  5. Guide To Online Shopping Uk Cheap: The Intermediate Guide For Online Shopping Uk Cheap online shopping uk

  6. cs2 case表示:

    Why Do So Many People Are Attracted To Why Are CSGO Skins Going Up In Price?
    cs2 case

  7. 7 Things About Medical Malpractice Law You’ll Kick Yourself For Not Knowing medical malpractice law firm [http://www.Mallangpeach.Com]

  8. Slot Demo Xo表示:

    What Makes The Slot Demo Gratis So Effective? For COVID-19 Slot Demo Xo

  9. Alejandrina表示:

    What’s The Job Market For Window Doctor Near Me Professionals Like?
    window doctor near me [Alejandrina]

  10. RobertAgish表示:

    African elephants use names to call each other, study suggests
    трипскан darknet
    Wild African elephants may address each other using individualized calls that resemble the personal names used by humans, a new study suggests.

    While dolphins are known to call one another by mimicking the signature whistle of the dolphin they want to address, and parrots have been found to address each other in a similar way, African elephants in Kenya may go a step further in identifying one another.

    These elephants learn, recognize and use individualized name-like calls to address others of their kind, seemingly without using imitation, according to the study published Monday in the journal Nature Ecology and Evolution.
    The most common type of elephant call is a rumble, of which there are three sub-categories. So-called contact rumbles are used to call another elephant that is far away or out of sight. Greeting rumbles are used when another elephant is within touching distance. Caregiver rumbles are used by an adolescent or adult female toward a calf she is caring for, according to the study.

    The researchers looked at these three types of rumbles, using a machine-learning model to analyze recordings of 469 calls made by wild groups of females and calves in Amboseli National Park and Samburu and Buffalo Springs National Reserves between 1986 and 2022. All the elephants could be individually identified by the shape of their ears, as they had been monitored continuously for decades, according to the study.

    The idea was that “if the calls contained something like a name, then you should be able to figure out who the call was addressed to just from the acoustic features of the call itself,” said lead study author Mickey Pardo, an animal behaviorist and postdoctoral fellow at Cornell University in New York.

    The researchers found that the acoustic structure of calls varied depending on who the target of the call was.

    The machine-learning model correctly identified the recipient of 27.5% of calls analyzed, “which may not sound like that much, but it was significantly more than what the model would have been able to do if we had just fed it random data,” Pardo told CNN.

    “So that suggests that there’s something in the calls that’s allowing the model to identify who the intended receiver of the call was,” he added.

  11. How Double Glazing Glass Replacement Near Me
    Rose To Become The #1 Trend On Social Media replace double Glazing glass

  12. Anibal表示:

    Where Do You Think Double Glazed Units Manufacturers Near Me Be One Year From
    Today? repair to double glazed windows (Anibal)

  13. Gudrun表示:

    The 10 Most Terrifying Things About Free Spin Slots free spin slots (Gudrun)

  14. Medicine information for patients. Long-Term Effects.
    how to get cheap imodium online
    Everything what you want to know about drug. Get now.

  15. Margareta表示:

    Ten Easy Steps To Launch The Business You Want To Start Double Glazed Replacement Windows
    Business glass replacement for window (Margareta)

  16. bbs.Ts3sv.com表示:

    7 Simple Secrets To Completely Rocking Your Lexus Key Replacement lexus key cost
    (bbs.Ts3sv.com)

  17. What’s The Job Market For Slot Trends Professionals Like?
    Real casino slots – images.google.bg

  18. Faucet repair表示:

    Another style of faucet is the compression valve faucet.

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

    If you have any recommendations, please share. Appreciate it!

  20. 9 Things Your Parents Teach You About Personal Injury Lawsuit Personal Injury

  21. See What Double Glazing Repairs Near Me Tricks The Celebs Are
    Using double glazing repairs near me – italystate9.werite.net

  22. sell meth表示:

    Your method of explaining everything in this piece of writing
    is actually good, all be able to easily know it, Thanks a lot.

  23. Spot on with this write-up, I seriously believe this amazing site needs far more attention. I’ll probably
    be back again to read more, thanks for the information!

  24. The world of science is full of exhilarating discoveries and mind-boggling phenomena. One such phenomenon that has captured the attention of scientists and researchers is http://www.thedreammate.com/home/bbs/board.php?bo_table=free&wr_id=1348734.

  25. What Is Mercedes Key And Why Is Everyone Dissing It?
    Replacement mercedes car key

  26. These are actually enormous ideas in regarding blogging.
    You have touched some pleasant points here. Any way keep up wrinting.

    Also visit my webpage lung clear pro reviews

  27. A Glimpse In New Slots Online’s Secrets Of New Slots Online reputable slots

  28. Many thanks! Plenty of advice!

發佈留言

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