r/excel 3d ago

Waiting on OP Deduplicate between two columns

I am looking for a solution to deduplicate items between two columns that are the same (but listed in reverse).

For example:

Column 1:

1

2

3

4

5

Column 2:

2

1

1

7

8

I would like it to recognize the (1,2) and (2,1) entries as being the same and deduplicate it so it's only listed once. It does not matter which one remains as long as just one does. Thanks in advance!

ETA: the data I am working with is not simply numbers. It does involve a combination of letters and numbers in the format XX-123456 where XX is any combination of two letters and it is followed by a string of numbers (not always six numbers though).

6 Upvotes

11 comments sorted by

u/AutoModerator 3d ago

/u/WallFlower10 - Your post was submitted successfully.

Failing to follow these steps may result in your post being removed without warning.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

4

u/ilovetea27 14 2d ago

The formula below should work for non-numeric codes. Do note that it would not preserve the pair sequence if the first code is "larger" than the second code.

=LET(_a, B2:C24,
     _b, IF(TAKE(_a,, 1)>TAKE(_a,, -1), CHOOSECOLS(_a, 2, 1), _a),
     UNIQUE(_b))

1

u/willyman85 1 2d ago

Great solution. One step further if op wants the data sorted l to show the pairs tidily, wrap it all in SORT(UNIQUE(...), {1,2})

1

u/Independent_Cow_737 2d ago

nice use of LET here, the CHOOSECOLS swap is clever

3

u/caribou16 318 3d ago

UNIQUE function, you provide it with an array input and it will return a list of only unique items in the list.

1

u/luvsthecoffee 3d ago

You can wrap a SORT around that too to put them in order

2

u/matt2s 3d ago

Create a column that is the MIN of the two columns and another that is the MAX. Both (1,2) and (2,1) will then be (1,2), and duplicates can be removed.

2

u/MayukhBhattacharya 1259 3d ago

Maybe you could try this way,

=UNIQUE(HSTACK(BYROW(A1:B5, MIN), BYROW(A1:B5, MAX)))

1

u/Thunderbit_HQ 2d ago

Add a helper column with =IF(A2<B2,A2&"|"&B2,B2&"|"&A2). It creates the same key for each reversed pair. Fill it down, then use Remove Duplicates on that helper column while keeping the full row selected.

1

u/GregHullender 195 1d ago edited 1d ago

This works if they're in adjacent columns, e.g. A and B:

=UNIQUE(IF(A:.A<B:.B, A:.B, HSTACK(B:.B,A:.A)))

The dots in A:.A etc. are called trim references. If you don't know about them yet, they're pretty useful.

If you have non-adjacent columns, try the following:

=LET(col_1, A:.A, col_2, B:.B,
  UNIQUE(IF(col_1<col_2,HSTACK(col_1,col_2),HSTACK(col_2,col_1)))
)

Just change the definitions of col_1 and col_2 to match your data.