Get values with no duplicates

Hi, I wanted to get data of an item that has no duplicate.

Here is an example of the data I got using merge node

I want to get ID 6998874, since it doesn’t have a duplicate value. How can I go about this using function node?

Hey @zocket,

Do you just want that one value or do you want to remove all duplicates (the item list node can do this)?

i needed the one value, that has no duplicates

Here is an example data. I want to only get values 15 and 16

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      id: 12,\n    }\n  },\n  {\n    json: {\n      id: 13,\n    }\n  },\n  {\n    json: {\n      id: 14,\n    }\n  },\n  {\n    json: {\n      id: 12,\n    }\n  },\n  {\n    json: {\n      id: 13,\n    }\n  },\n  {\n    json: {\n      id: 14,\n    }\n  },\n  {\n    json: {\n      id: 15,\n    }\n  },\n  {\n    json: {\n      id: 16,\n    }\n  }\n]\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @zocket, I was having a look at the example data provided and tried to keep the non-duplicate values using the Merge and Item Lists node but couldn’t come up with a working approach that would only let you keep unique values.

So unless someone else has a nice idea for this that has not come to my mind I’d write a quick helper function counting the occurrences of each ID. You could then use the IF node to filter those with only one occurrence.

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      id: 12,\n    }\n  },\n  {\n    json: {\n      id: 13,\n    }\n  },\n  {\n    json: {\n      id: 14,\n    }\n  },\n  {\n    json: {\n      id: 12,\n    }\n  },\n  {\n    json: {\n      id: 13,\n    }\n  },\n  {\n    json: {\n      id: 14,\n    }\n  },\n  {\n    json: {\n      id: 15,\n    }\n  },\n  {\n    json: {\n      id: 16,\n    }\n  }\n]\n"
      },
      "name": "Set Example Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "for (item of items) {\n  item.json.count = items.filter(e => e.json.id == item.json.id).length;\n}\n\nreturn items;"
      },
      "name": "Count ID Occurrence",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Example Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Example Data": {
      "main": [
        [
          {
            "node": "Count ID Occurrence",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Does this help you?

1 Like

Thanks so much. This has helped solved my issue.

1 Like

Nice, glad to hear that (though it bothers me I couldn’t come with a code-free approach :see_no_evil:).

Thanks so much for confirming!

1 Like