How to find duplicates in an array and put them in a new array

Hello!
I want to find duplicates in array below and put them into a new array. No sorting.

Array:
apple, plum, banana, apple, plum, orange, apple, apricot, strawberry

Result array:
apple, plum
or
plum, apple

How to do it in a simple way?

Hey @garry

You can create a set variable module and put all elements, after that you use iterator to iterate the proces.
create two arrays - seen and duplicate (keep them empty initially). Using filters saturate the elements and check if the element exists or not.

Not really understand your idea.
By the way I have several arrays with duplicates and should create copies of them with duplicates only.

Welcome to the Make community!

All you need is to “filter” the grouped arrays that has more than one item:

Output

Screenshot_2024-07-18_150723

Module Export

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the JSON code below by clicking the copy button when you mouseover the top-right of the code block
    Screenshot_2024-01-17_200117

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the canvas.

  3. Click on each imported module and save it for validation. You may be prompted to remap some variables and connections.

Click to Expand Module Export Code

JSON - Copy and Paste this directly in the scenario editor

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 146,
                    "module": "util:SetVariables",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "scope": "roundtrip",
                        "variables": [
                            {
                                "name": "array1",
                                "value": "{{split(\"apple, plum, banana\"; \", \")}}"
                            },
                            {
                                "name": "array2",
                                "value": "{{split(\"apple, plum, orange\"; \", \")}}"
                            },
                            {
                                "name": "array3",
                                "value": "{{split(\"apple, apricot, strawberry\"; \", \")}}"
                            }
                        ]
                    },
                    "metadata": {
                        "designer": {
                            "x": 2025,
                            "y": -1105,
                            "name": "Three Arrays"
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                },
                                "variables": {
                                    "items": [
                                        null,
                                        null,
                                        null
                                    ]
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "variables",
                                "spec": [
                                    {
                                        "name": "name",
                                        "type": "text",
                                        "label": "Variable name",
                                        "required": true
                                    },
                                    {
                                        "name": "value",
                                        "type": "any",
                                        "label": "Variable value"
                                    }
                                ],
                                "type": "array",
                                "label": "Variables"
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            }
                        ],
                        "interface": [
                            {
                                "name": "array1",
                                "type": "any",
                                "label": "array1"
                            },
                            {
                                "name": "array2",
                                "type": "any",
                                "label": "array2"
                            },
                            {
                                "name": "array3",
                                "type": "any",
                                "label": "array3"
                            }
                        ]
                    }
                },
                {
                    "id": 147,
                    "module": "builtin:BasicFeeder",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "array": "{{merge(146.array1; 146.array2; 146.array3)}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 2270,
                            "y": -1098,
                            "name": "Merge, Iterate Items"
                        },
                        "restore": {
                            "expect": {
                                "array": {
                                    "mode": "edit"
                                }
                            }
                        },
                        "expect": [
                            {
                                "mode": "edit",
                                "name": "array",
                                "spec": [],
                                "type": "array",
                                "label": "Array"
                            }
                        ]
                    }
                },
                {
                    "id": 148,
                    "module": "builtin:BasicAggregator",
                    "version": 1,
                    "parameters": {
                        "feeder": 147
                    },
                    "mapper": {
                        "value": "{{147.value}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 2515,
                            "y": -1098,
                            "name": "Group by Value"
                        },
                        "restore": {
                            "extra": {
                                "feeder": {
                                    "label": "Merge, Iterate Items [147]"
                                },
                                "target": {
                                    "label": "Custom"
                                }
                            }
                        },
                        "advanced": true
                    },
                    "flags": {
                        "groupBy": "{{147.value}}"
                    }
                },
                {
                    "id": 153,
                    "module": "util:TextAggregator",
                    "version": 1,
                    "parameters": {
                        "rowSeparator": "other",
                        "otherRowSeparator": ", ",
                        "feeder": 148
                    },
                    "filter": {
                        "name": "Has Dupes",
                        "conditions": [
                            [
                                {
                                    "a": "{{148.array}}",
                                    "o": "array:greater",
                                    "b": "1"
                                }
                            ]
                        ]
                    },
                    "mapper": {
                        "value": "{{148.`__IMTKEY__`}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 2821,
                            "y": -1099,
                            "name": "List Of Duplicate Items",
                            "messages": [
                                {
                                    "category": "last",
                                    "severity": "warning",
                                    "message": "A transformer should not be the last module in the route."
                                }
                            ]
                        },
                        "restore": {
                            "parameters": {
                                "rowSeparator": {
                                    "label": "Other"
                                }
                            },
                            "extra": {
                                "feeder": {
                                    "label": "Group by Value [148]"
                                }
                            },
                            "flags": {
                                "groupBy": {
                                    "collapsed": true
                                },
                                "stopIfEmpty": {
                                    "collapsed": true
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "rowSeparator",
                                "type": "select",
                                "label": "Row separator",
                                "validate": {
                                    "enum": [
                                        "\n",
                                        "\t",
                                        "other"
                                    ]
                                }
                            },
                            {
                                "name": "otherRowSeparator",
                                "type": "text",
                                "label": "Separator"
                            }
                        ],
                        "expect": [
                            {
                                "name": "value",
                                "type": "text",
                                "label": "Text"
                            }
                        ],
                        "advanced": true
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}
4 Likes

Scenario stops on filter.


You selected the wrong array variable.

Copy and paste my module export.

samliew – request private consultation

Join the unofficial Make Discord server to chat with us!

Thanks! You are very helpful!

1 Like