Home >Backend Development >Golang >How to delete duplicate interface data in {}interface{}

How to delete duplicate interface data in {}interface{}

PHPz
PHPzforward
2024-02-09 23:12:091016browse

How to delete duplicate interface data in {}interface{}

php editor Youzi will introduce to you how to delete duplicate interface data{} in the {}interface. During the development process, we often encounter situations where we need to delete duplicate data. Duplicate data not only affects system performance, but also causes trouble to users. In order to solve this problem, we can use some techniques and methods to detect and delete duplicate interface data, thereby improving system efficiency and user experience. Next, we’ll give you the details on how to achieve this.

Question content

How to remove duplicate interface{} entries from []interface{} data.

"data": [
        {
            "signstatus": true,
            "username": "yash90"
        },
        {
            "signstatus": false,
            "username": "dhananjay"
        },
        {
            "signstatus": true,
            "username": "yash90"
        }
    ],

Currently, I have the above data and I want to remove duplicate entries for the same username. So what kind of logic do I need in the function below.

func removeduplicateentries(listofusernamesandsignstatus []interface{}) []interface{} {
    updatedlistofusernamesandsignstatus := make([]interface{}, 0)
    for _, value := range listofusernamesandsignstatus {
        if value != nil {
        updatedlistofusernamesandsignstatus = append(updatedlistofusernamesandsignstatus, value)
        }
    }
    return updatedlistofusernamesandsignstatus
}

Any idea how to do this?

I expect the result should be as follows:

"data": [
        {
            "signstatus": true,
            "username": "Yash90"
        },
        {
            "signstatus": false,
            "username": "Dhananjay"
        },
    ],

Workaround

First of all, I think using []interface{} as input is a bad idea in this specific case. The correct input should be []*entry with

type entry struct {
    username   string `json:"username"`
    signstatus bool   `json:"signstatus"`
}

Or at least map[string]interface{}.

But if []interface{} is a must, then here is the solution.

func removeDuplicateEntries(entries []interface{}) []interface{} {
    uniqueEntries := make([]interface{}, 0)
    usernameMap := make(map[string]bool)
    for _, entry := range entries {
        entryCast, ok := entry.(map[string]interface{})
        if !ok {
            continue
        }
        username := entryCast["username"].(string)
        if usernameMap[username] {
            continue
        }
        usernameMap[username] = true
        uniqueEntries = append(uniqueEntries, entry)
    }

    return uniqueEntries
}

The above is the detailed content of How to delete duplicate interface data in {}interface{}. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete