How to find items tied for most appearances in a list?
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
I want to create a list of the items in list w
tied for appearing the most times in w
. In other words, if 22 appears in w
more than any other number, I want the result to be 22
. If 34 and 55 appear the most times in w
but the same number of times, I want the result to be 34,55
.
The example below just uses a randomly generated list w
. My method works but is ugly and inefficient.
w = RandomInteger[100, 200]
fw = w // DeleteDuplicates
wc = Counts[w]
m = Max[fw /. wc]
Reap[Do[If[(fw[[i]] /. wc) == m, Sow[fw[[i]]]], i, 1, Length[fw]]][[2, 1]]
Is there a tidier way?
list-manipulation
add a comment |Â
up vote
8
down vote
favorite
I want to create a list of the items in list w
tied for appearing the most times in w
. In other words, if 22 appears in w
more than any other number, I want the result to be 22
. If 34 and 55 appear the most times in w
but the same number of times, I want the result to be 34,55
.
The example below just uses a randomly generated list w
. My method works but is ugly and inefficient.
w = RandomInteger[100, 200]
fw = w // DeleteDuplicates
wc = Counts[w]
m = Max[fw /. wc]
Reap[Do[If[(fw[[i]] /. wc) == m, Sow[fw[[i]]]], i, 1, Length[fw]]][[2, 1]]
Is there a tidier way?
list-manipulation
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I want to create a list of the items in list w
tied for appearing the most times in w
. In other words, if 22 appears in w
more than any other number, I want the result to be 22
. If 34 and 55 appear the most times in w
but the same number of times, I want the result to be 34,55
.
The example below just uses a randomly generated list w
. My method works but is ugly and inefficient.
w = RandomInteger[100, 200]
fw = w // DeleteDuplicates
wc = Counts[w]
m = Max[fw /. wc]
Reap[Do[If[(fw[[i]] /. wc) == m, Sow[fw[[i]]]], i, 1, Length[fw]]][[2, 1]]
Is there a tidier way?
list-manipulation
I want to create a list of the items in list w
tied for appearing the most times in w
. In other words, if 22 appears in w
more than any other number, I want the result to be 22
. If 34 and 55 appear the most times in w
but the same number of times, I want the result to be 34,55
.
The example below just uses a randomly generated list w
. My method works but is ugly and inefficient.
w = RandomInteger[100, 200]
fw = w // DeleteDuplicates
wc = Counts[w]
m = Max[fw /. wc]
Reap[Do[If[(fw[[i]] /. wc) == m, Sow[fw[[i]]]], i, 1, Length[fw]]][[2, 1]]
Is there a tidier way?
list-manipulation
list-manipulation
edited yesterday
asked 2 days ago
Jerry Guern
1,954732
1,954732
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
9
down vote
accepted
You need Commonest
:
SeedRandom[1]
w = RandomInteger[100, 200];
Commonest[w]
68, 25, 63
% /. Counts[w]
5, 5, 5
Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
â Jerry Guern
2 days ago
1
@JerryGuern, thank you for the accept. About the last line: it gives the same result as68, 25, 63 /. Counts[w]
.%
stands for the last result generated . (See Out (%) in the docs.) So the last line is the same asCount[w, #]&/@ 68,25,63
. See also: Map (/@), Function (&) and Slot (#).
â kglr
2 days ago
Okay, thanks for the followup and links.
â Jerry Guern
yesterday
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
You need Commonest
:
SeedRandom[1]
w = RandomInteger[100, 200];
Commonest[w]
68, 25, 63
% /. Counts[w]
5, 5, 5
Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
â Jerry Guern
2 days ago
1
@JerryGuern, thank you for the accept. About the last line: it gives the same result as68, 25, 63 /. Counts[w]
.%
stands for the last result generated . (See Out (%) in the docs.) So the last line is the same asCount[w, #]&/@ 68,25,63
. See also: Map (/@), Function (&) and Slot (#).
â kglr
2 days ago
Okay, thanks for the followup and links.
â Jerry Guern
yesterday
add a comment |Â
up vote
9
down vote
accepted
You need Commonest
:
SeedRandom[1]
w = RandomInteger[100, 200];
Commonest[w]
68, 25, 63
% /. Counts[w]
5, 5, 5
Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
â Jerry Guern
2 days ago
1
@JerryGuern, thank you for the accept. About the last line: it gives the same result as68, 25, 63 /. Counts[w]
.%
stands for the last result generated . (See Out (%) in the docs.) So the last line is the same asCount[w, #]&/@ 68,25,63
. See also: Map (/@), Function (&) and Slot (#).
â kglr
2 days ago
Okay, thanks for the followup and links.
â Jerry Guern
yesterday
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
You need Commonest
:
SeedRandom[1]
w = RandomInteger[100, 200];
Commonest[w]
68, 25, 63
% /. Counts[w]
5, 5, 5
You need Commonest
:
SeedRandom[1]
w = RandomInteger[100, 200];
Commonest[w]
68, 25, 63
% /. Counts[w]
5, 5, 5
edited yesterday
answered 2 days ago
kglr
159k8184384
159k8184384
Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
â Jerry Guern
2 days ago
1
@JerryGuern, thank you for the accept. About the last line: it gives the same result as68, 25, 63 /. Counts[w]
.%
stands for the last result generated . (See Out (%) in the docs.) So the last line is the same asCount[w, #]&/@ 68,25,63
. See also: Map (/@), Function (&) and Slot (#).
â kglr
2 days ago
Okay, thanks for the followup and links.
â Jerry Guern
yesterday
add a comment |Â
Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
â Jerry Guern
2 days ago
1
@JerryGuern, thank you for the accept. About the last line: it gives the same result as68, 25, 63 /. Counts[w]
.%
stands for the last result generated . (See Out (%) in the docs.) So the last line is the same asCount[w, #]&/@ 68,25,63
. See also: Map (/@), Function (&) and Slot (#).
â kglr
2 days ago
Okay, thanks for the followup and links.
â Jerry Guern
yesterday
Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
â Jerry Guern
2 days ago
Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
â Jerry Guern
2 days ago
1
1
@JerryGuern, thank you for the accept. About the last line: it gives the same result as
68, 25, 63 /. Counts[w]
. %
stands for the last result generated . (See Out (%) in the docs.) So the last line is the same as Count[w, #]&/@ 68,25,63
. See also: Map (/@), Function (&) and Slot (#).â kglr
2 days ago
@JerryGuern, thank you for the accept. About the last line: it gives the same result as
68, 25, 63 /. Counts[w]
. %
stands for the last result generated . (See Out (%) in the docs.) So the last line is the same as Count[w, #]&/@ 68,25,63
. See also: Map (/@), Function (&) and Slot (#).â kglr
2 days ago
Okay, thanks for the followup and links.
â Jerry Guern
yesterday
Okay, thanks for the followup and links.
â Jerry Guern
yesterday
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181652%2fhow-to-find-items-tied-for-most-appearances-in-a-list%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password