1
$\begingroup$

This question already has an answer here:

I have a list:

list = {{7, 6, 14, 14, 4}, {4, 9, 8, 11, 14}, {20, 12, 12, 11, 3}, {7,
4, 11, 7, 20}, {2, 13, 17, 11, 19}}

I did a test to identify where duplicates occur. So I created only subsets of list [[1]]:

sub = Subsets[list[[1]], {2}]

$\left( \begin{array}{cc} 7 & 6 \\ 7 & 14 \\ 7 & 14 \\ 7 & 4 \\ 6 & 14 \\ 6 & 14 \\ 6 & 4 \\ 14 & 14 \\ 14 & 4 \\ 14 & 4 \\ \end{array} \right)$

I tried to identify where the duplicates occur:

sub[[#1, 1]] == sub[[#1, 2]] & /@ Range[Length[sub]]

{False, False, False, False, False, False, False, True, False, False}

I noticed that it exists, but why using Select is not selected ${14,14}$?

Select[list, sub[[#1, 1]] == sub[[#1, 2]] & /@ Range[Length[sub]]]

$\{\}$

EDIT

I made some modifications based on the answers:

So I find out which lists there are duplicates:

Not[DuplicateFreeQ[list[[#]]]] & /@ Range[Length[list]]

{True, False, True, True, False}

But still Select does not work the way I want it:

Select[list, Not[DuplicateFreeQ[list[[#]]]] & /@ Range[Length[list]]]

$\{\}$

I want to get the lists that have duplicate parts, like this:

$\left( \begin{array}{cc} 7 & 6 & 14 & 14 & 4 \\ 20 & 12 & 12 & 11 & 3 \\ 7 & 4 & 11 & 7 & 20 \\ \end{array} \right)$

$\endgroup$

marked as duplicate by corey979, LCarvalho, Feyre, MarcoB, m_goldberg Jan 4 '17 at 23:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Select[sub, Equal @@ # &] $\endgroup$ – Bob Hanlon Jan 4 '17 at 17:44
2
$\begingroup$

You can delete duplicate cases:

list = {{7, 6, 14, 14, 4}, {4, 9, 8, 11, 14}, {20, 12, 12, 11, 3}, {7,
    4, 11, 7, 20}, {2, 13, 17, 11, 19}}

$\left( \begin{array}{ccccc} 7 & 6 & 14 & 14 & 4 \\ 4 & 9 & 8 & 11 & 14 \\ 20 & 12 & 12 & 11 & 3 \\ 7 & 4 & 11 & 7 & 20 \\ 2 & 13 & 17 & 11 & 19 \\ \end{array} \right)$

Delete[list, 
 Position[Boole[DuplicateFreeQ[list[[#]]] & /@ Range[Length[list]]], 
  1]]

$\left( \begin{array}{ccccc} 7 & 6 & 14 & 14 & 4 \\ 20 & 12 & 12 & 11 & 3 \\ 7 & 4 & 11 & 7 & 20 \\ \end{array} \right)$

$\endgroup$
4
$\begingroup$
Pick[list, DuplicateFreeQ /@ list, False]
 (* or *)
Select[list, DuplicateFreeQ@# == False &]

{{7, 6, 14, 14, 4}, {20, 12, 12, 11, 3}, {7, 4, 11, 7, 20}}

$\endgroup$
4
$\begingroup$

In your case you can eliminate simply the duplicate by

DeleteDuplicates[list[[1]]]

but you perhaps this may be a part of a huge procedure and you do not know which numbers are duplicates. This is easy found with

Tally[list[[1]]]

which gives the order of multiplicity for each element.

EDIT

There is certainly a better way

list = {{7, 6, 14, 14, 4}, {4, 9, 8, 11, 14}, {20, 12, 12, 11, 3}, {7,
     4, 11, 7, 20}, {2, 13, 17, 11, 19}};
dup = Table[Tally[list[[i]]], {i, 1, Length[list]}];
m = DeleteCases[
   Table[If[dup[[All, All, 2]][[i]] == {1, 1, 1, 1, 1}, 0, 
     list[[i]]], {i, 1, Length[list]}], 0];
m // MatrixForm
$\endgroup$
3
$\begingroup$

Here are two possibilities:

Pick[list, Replace[list, {{___, x_, ___, x_, ___} :> True, _ -> False}, {1}]]
Extract[list, Position[list, {___, x_, ___, x_, ___}]]

I found that the most elegant thing I could come up with was the pattern-matching approach, but it is bound to be slower if the sublists are long.

$\endgroup$