How to remove the item that is repeated?
$begingroup$
If i have a
list={m,n,p,q,q,r,l,l}
how can i remove the repeated item with pattern matching so i would have
modifiedlist={m,n,p,r}?
pattern-matching
$endgroup$
add a comment |
$begingroup$
If i have a
list={m,n,p,q,q,r,l,l}
how can i remove the repeated item with pattern matching so i would have
modifiedlist={m,n,p,r}?
pattern-matching
$endgroup$
2
$begingroup$
{a, b, a} - isa
repeated here?
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
@Kuba yes it is.
$endgroup$
– user49047
1 hour ago
2
$begingroup$
Closely related: mathematica.stackexchange.com/q/40317/5478
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
Also related mathematica.stackexchange.com/q/107483/106
$endgroup$
– user1066
28 mins ago
add a comment |
$begingroup$
If i have a
list={m,n,p,q,q,r,l,l}
how can i remove the repeated item with pattern matching so i would have
modifiedlist={m,n,p,r}?
pattern-matching
$endgroup$
If i have a
list={m,n,p,q,q,r,l,l}
how can i remove the repeated item with pattern matching so i would have
modifiedlist={m,n,p,r}?
pattern-matching
pattern-matching
asked 1 hour ago
user49047user49047
19510
19510
2
$begingroup$
{a, b, a} - isa
repeated here?
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
@Kuba yes it is.
$endgroup$
– user49047
1 hour ago
2
$begingroup$
Closely related: mathematica.stackexchange.com/q/40317/5478
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
Also related mathematica.stackexchange.com/q/107483/106
$endgroup$
– user1066
28 mins ago
add a comment |
2
$begingroup$
{a, b, a} - isa
repeated here?
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
@Kuba yes it is.
$endgroup$
– user49047
1 hour ago
2
$begingroup$
Closely related: mathematica.stackexchange.com/q/40317/5478
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
Also related mathematica.stackexchange.com/q/107483/106
$endgroup$
– user1066
28 mins ago
2
2
$begingroup$
{a, b, a} - is
a
repeated here?$endgroup$
– Kuba♦
1 hour ago
$begingroup$
{a, b, a} - is
a
repeated here?$endgroup$
– Kuba♦
1 hour ago
$begingroup$
@Kuba yes it is.
$endgroup$
– user49047
1 hour ago
$begingroup$
@Kuba yes it is.
$endgroup$
– user49047
1 hour ago
2
2
$begingroup$
Closely related: mathematica.stackexchange.com/q/40317/5478
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
Closely related: mathematica.stackexchange.com/q/40317/5478
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
Also related mathematica.stackexchange.com/q/107483/106
$endgroup$
– user1066
28 mins ago
$begingroup$
Also related mathematica.stackexchange.com/q/107483/106
$endgroup$
– user1066
28 mins ago
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
Select[list, Count[list, #] == 1 &]
{m, n, p, r}
Also
Select[list, Counts[list][#] == 1 &]
{m, n, p, r}
Keys@Select[Counts[list], # == 1 &]
{m, n, p, r}
$endgroup$
$begingroup$
very nice i added {m,n,p,q,q,r,l,l} /. a_List :> Select[a, Count[a, #] == 1 &]
$endgroup$
– user49047
1 hour ago
$begingroup$
@user49047,Select[a, Count[a, #] == 1 &]
gives you the result directly; you don't need to useReplaceAll
.
$endgroup$
– kglr
1 hour ago
$begingroup$
The method usingCounts
is probably the best way, since you only have to traverse the whole list once.
$endgroup$
– Sjoerd Smit
1 hour ago
$begingroup$
@SjoerdSmit, excellent point.
$endgroup$
– kglr
1 hour ago
add a comment |
$begingroup$
First /@ Select[Tally@list, Last@# == 1 &]
{m, n, p, r}
or
First /@ Cases[Tally@list, {_, 1}]
{m, n, p, r}
$endgroup$
1
$begingroup$
Cases
seems to be fastest so far on large lists.Cases[Tally@list, {_, 1}][[All, 1]]
is about 10% faster on unpacked arrays, slightly slower on packed arrays.
$endgroup$
– Michael E2
50 mins ago
add a comment |
$begingroup$
DeleteDuplicates[{m, n, p, q, q, r, l, l}]
(*{m, n, p, q, r, l}*)
$endgroup$
2
$begingroup$
@SjoerdSmit It clearly does not do what the OP wants, which is to remove all items that are duplicated, not just the extra ones. Compare with the desired output in the OP.
$endgroup$
– Michael E2
1 hour ago
$begingroup$
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
$endgroup$
– corey979
43 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f190074%2fhow-to-remove-the-item-that-is-repeated%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Select[list, Count[list, #] == 1 &]
{m, n, p, r}
Also
Select[list, Counts[list][#] == 1 &]
{m, n, p, r}
Keys@Select[Counts[list], # == 1 &]
{m, n, p, r}
$endgroup$
$begingroup$
very nice i added {m,n,p,q,q,r,l,l} /. a_List :> Select[a, Count[a, #] == 1 &]
$endgroup$
– user49047
1 hour ago
$begingroup$
@user49047,Select[a, Count[a, #] == 1 &]
gives you the result directly; you don't need to useReplaceAll
.
$endgroup$
– kglr
1 hour ago
$begingroup$
The method usingCounts
is probably the best way, since you only have to traverse the whole list once.
$endgroup$
– Sjoerd Smit
1 hour ago
$begingroup$
@SjoerdSmit, excellent point.
$endgroup$
– kglr
1 hour ago
add a comment |
$begingroup$
Select[list, Count[list, #] == 1 &]
{m, n, p, r}
Also
Select[list, Counts[list][#] == 1 &]
{m, n, p, r}
Keys@Select[Counts[list], # == 1 &]
{m, n, p, r}
$endgroup$
$begingroup$
very nice i added {m,n,p,q,q,r,l,l} /. a_List :> Select[a, Count[a, #] == 1 &]
$endgroup$
– user49047
1 hour ago
$begingroup$
@user49047,Select[a, Count[a, #] == 1 &]
gives you the result directly; you don't need to useReplaceAll
.
$endgroup$
– kglr
1 hour ago
$begingroup$
The method usingCounts
is probably the best way, since you only have to traverse the whole list once.
$endgroup$
– Sjoerd Smit
1 hour ago
$begingroup$
@SjoerdSmit, excellent point.
$endgroup$
– kglr
1 hour ago
add a comment |
$begingroup$
Select[list, Count[list, #] == 1 &]
{m, n, p, r}
Also
Select[list, Counts[list][#] == 1 &]
{m, n, p, r}
Keys@Select[Counts[list], # == 1 &]
{m, n, p, r}
$endgroup$
Select[list, Count[list, #] == 1 &]
{m, n, p, r}
Also
Select[list, Counts[list][#] == 1 &]
{m, n, p, r}
Keys@Select[Counts[list], # == 1 &]
{m, n, p, r}
edited 1 hour ago
answered 1 hour ago
kglrkglr
180k9200413
180k9200413
$begingroup$
very nice i added {m,n,p,q,q,r,l,l} /. a_List :> Select[a, Count[a, #] == 1 &]
$endgroup$
– user49047
1 hour ago
$begingroup$
@user49047,Select[a, Count[a, #] == 1 &]
gives you the result directly; you don't need to useReplaceAll
.
$endgroup$
– kglr
1 hour ago
$begingroup$
The method usingCounts
is probably the best way, since you only have to traverse the whole list once.
$endgroup$
– Sjoerd Smit
1 hour ago
$begingroup$
@SjoerdSmit, excellent point.
$endgroup$
– kglr
1 hour ago
add a comment |
$begingroup$
very nice i added {m,n,p,q,q,r,l,l} /. a_List :> Select[a, Count[a, #] == 1 &]
$endgroup$
– user49047
1 hour ago
$begingroup$
@user49047,Select[a, Count[a, #] == 1 &]
gives you the result directly; you don't need to useReplaceAll
.
$endgroup$
– kglr
1 hour ago
$begingroup$
The method usingCounts
is probably the best way, since you only have to traverse the whole list once.
$endgroup$
– Sjoerd Smit
1 hour ago
$begingroup$
@SjoerdSmit, excellent point.
$endgroup$
– kglr
1 hour ago
$begingroup$
very nice i added {m,n,p,q,q,r,l,l} /. a_List :> Select[a, Count[a, #] == 1 &]
$endgroup$
– user49047
1 hour ago
$begingroup$
very nice i added {m,n,p,q,q,r,l,l} /. a_List :> Select[a, Count[a, #] == 1 &]
$endgroup$
– user49047
1 hour ago
$begingroup$
@user49047,
Select[a, Count[a, #] == 1 &]
gives you the result directly; you don't need to use ReplaceAll
.$endgroup$
– kglr
1 hour ago
$begingroup$
@user49047,
Select[a, Count[a, #] == 1 &]
gives you the result directly; you don't need to use ReplaceAll
.$endgroup$
– kglr
1 hour ago
$begingroup$
The method using
Counts
is probably the best way, since you only have to traverse the whole list once.$endgroup$
– Sjoerd Smit
1 hour ago
$begingroup$
The method using
Counts
is probably the best way, since you only have to traverse the whole list once.$endgroup$
– Sjoerd Smit
1 hour ago
$begingroup$
@SjoerdSmit, excellent point.
$endgroup$
– kglr
1 hour ago
$begingroup$
@SjoerdSmit, excellent point.
$endgroup$
– kglr
1 hour ago
add a comment |
$begingroup$
First /@ Select[Tally@list, Last@# == 1 &]
{m, n, p, r}
or
First /@ Cases[Tally@list, {_, 1}]
{m, n, p, r}
$endgroup$
1
$begingroup$
Cases
seems to be fastest so far on large lists.Cases[Tally@list, {_, 1}][[All, 1]]
is about 10% faster on unpacked arrays, slightly slower on packed arrays.
$endgroup$
– Michael E2
50 mins ago
add a comment |
$begingroup$
First /@ Select[Tally@list, Last@# == 1 &]
{m, n, p, r}
or
First /@ Cases[Tally@list, {_, 1}]
{m, n, p, r}
$endgroup$
1
$begingroup$
Cases
seems to be fastest so far on large lists.Cases[Tally@list, {_, 1}][[All, 1]]
is about 10% faster on unpacked arrays, slightly slower on packed arrays.
$endgroup$
– Michael E2
50 mins ago
add a comment |
$begingroup$
First /@ Select[Tally@list, Last@# == 1 &]
{m, n, p, r}
or
First /@ Cases[Tally@list, {_, 1}]
{m, n, p, r}
$endgroup$
First /@ Select[Tally@list, Last@# == 1 &]
{m, n, p, r}
or
First /@ Cases[Tally@list, {_, 1}]
{m, n, p, r}
answered 1 hour ago
JerryJerry
1,142112
1,142112
1
$begingroup$
Cases
seems to be fastest so far on large lists.Cases[Tally@list, {_, 1}][[All, 1]]
is about 10% faster on unpacked arrays, slightly slower on packed arrays.
$endgroup$
– Michael E2
50 mins ago
add a comment |
1
$begingroup$
Cases
seems to be fastest so far on large lists.Cases[Tally@list, {_, 1}][[All, 1]]
is about 10% faster on unpacked arrays, slightly slower on packed arrays.
$endgroup$
– Michael E2
50 mins ago
1
1
$begingroup$
Cases
seems to be fastest so far on large lists. Cases[Tally@list, {_, 1}][[All, 1]]
is about 10% faster on unpacked arrays, slightly slower on packed arrays.$endgroup$
– Michael E2
50 mins ago
$begingroup$
Cases
seems to be fastest so far on large lists. Cases[Tally@list, {_, 1}][[All, 1]]
is about 10% faster on unpacked arrays, slightly slower on packed arrays.$endgroup$
– Michael E2
50 mins ago
add a comment |
$begingroup$
DeleteDuplicates[{m, n, p, q, q, r, l, l}]
(*{m, n, p, q, r, l}*)
$endgroup$
2
$begingroup$
@SjoerdSmit It clearly does not do what the OP wants, which is to remove all items that are duplicated, not just the extra ones. Compare with the desired output in the OP.
$endgroup$
– Michael E2
1 hour ago
$begingroup$
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
$endgroup$
– corey979
43 mins ago
add a comment |
$begingroup$
DeleteDuplicates[{m, n, p, q, q, r, l, l}]
(*{m, n, p, q, r, l}*)
$endgroup$
2
$begingroup$
@SjoerdSmit It clearly does not do what the OP wants, which is to remove all items that are duplicated, not just the extra ones. Compare with the desired output in the OP.
$endgroup$
– Michael E2
1 hour ago
$begingroup$
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
$endgroup$
– corey979
43 mins ago
add a comment |
$begingroup$
DeleteDuplicates[{m, n, p, q, q, r, l, l}]
(*{m, n, p, q, r, l}*)
$endgroup$
DeleteDuplicates[{m, n, p, q, q, r, l, l}]
(*{m, n, p, q, r, l}*)
answered 1 hour ago
Ulrich NeumannUlrich Neumann
8,149516
8,149516
2
$begingroup$
@SjoerdSmit It clearly does not do what the OP wants, which is to remove all items that are duplicated, not just the extra ones. Compare with the desired output in the OP.
$endgroup$
– Michael E2
1 hour ago
$begingroup$
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
$endgroup$
– corey979
43 mins ago
add a comment |
2
$begingroup$
@SjoerdSmit It clearly does not do what the OP wants, which is to remove all items that are duplicated, not just the extra ones. Compare with the desired output in the OP.
$endgroup$
– Michael E2
1 hour ago
$begingroup$
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
$endgroup$
– corey979
43 mins ago
2
2
$begingroup$
@SjoerdSmit It clearly does not do what the OP wants, which is to remove all items that are duplicated, not just the extra ones. Compare with the desired output in the OP.
$endgroup$
– Michael E2
1 hour ago
$begingroup$
@SjoerdSmit It clearly does not do what the OP wants, which is to remove all items that are duplicated, not just the extra ones. Compare with the desired output in the OP.
$endgroup$
– Michael E2
1 hour ago
$begingroup$
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
$endgroup$
– corey979
43 mins ago
$begingroup$
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
$endgroup$
– corey979
43 mins ago
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f190074%2fhow-to-remove-the-item-that-is-repeated%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
$begingroup$
{a, b, a} - is
a
repeated here?$endgroup$
– Kuba♦
1 hour ago
$begingroup$
@Kuba yes it is.
$endgroup$
– user49047
1 hour ago
2
$begingroup$
Closely related: mathematica.stackexchange.com/q/40317/5478
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
Also related mathematica.stackexchange.com/q/107483/106
$endgroup$
– user1066
28 mins ago