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 Sep 11 at 8:55
asked Sep 11 at 0:55
Jerry Guern
1,954833
1,954833
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
Sep 11 at 4:21
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
Sep 11 at 4:38
Okay, thanks for the followup and links.
– Jerry Guern
Sep 11 at 8:51
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
Sep 11 at 4:21
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
Sep 11 at 4:38
Okay, thanks for the followup and links.
– Jerry Guern
Sep 11 at 8:51
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
Sep 11 at 4:21
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
Sep 11 at 4:38
Okay, thanks for the followup and links.
– Jerry Guern
Sep 11 at 8:51
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 Sep 11 at 6:24
answered Sep 11 at 1:00
kglr
170k8193397
170k8193397
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
Sep 11 at 4:21
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
Sep 11 at 4:38
Okay, thanks for the followup and links.
– Jerry Guern
Sep 11 at 8:51
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
Sep 11 at 4:21
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
Sep 11 at 4:38
Okay, thanks for the followup and links.
– Jerry Guern
Sep 11 at 8:51
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
Sep 11 at 4:21
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
Sep 11 at 4:21
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
Sep 11 at 4:38
@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
Sep 11 at 4:38
Okay, thanks for the followup and links.
– Jerry Guern
Sep 11 at 8:51
Okay, thanks for the followup and links.
– Jerry Guern
Sep 11 at 8:51
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