Jump to content
alpha993

Removing combinations from array

Recommended Posts

I've been scratching my head at a problem with arrays recently but I can't seem to find a decent solution to it.  So let's say I have an array that looks like this: [[1,2],[2,1],[3,4],[4,3]]. In this case, the first two elements are combinations of each other, same as the last two. What I am trying to do is to filter this array to remove such combinations and leave only one of them so that the array would look like [[1,2],[3,4]] instead. I'd greatly appreciate any help with this!

Share this post


Link to post
Share on other sites

What do you think about:

_array = [[1,2],[2,1],["zz","tt"],[4,3],["tt","zz"],[3,2,1]];

_result= [];
{_e = +_x; _e sort TRUE; _result pushBackUnique _e} forEach _array;   // [[1,2],["tt","zz"],[3,4],[1,2,3]]

Spoiler

 

Consolidated for array of anything:

_array = [[1,2],[2,1],["zz","tt"],[4,3],["tt","zz"],[3,2,1],6,"hello"];

_result= [];
{_e = _x; if (_e isEqualType []) then {_e = +_x; _e sort TRUE}; _result pushBackUnique _e} forEach _array;

 

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

Just tested it and it works wonders. Thanks so much!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×