How to remove duplicates from arrays with complex structures

 [
    [
        {"title": "11111111111", "field": "", "colspan": 2, "rowspan": "1"},
        {"title": "11111111111", "field": "", "colspan": 2, "rowspan": "1"},
        {"title": "22222222", "field": "", "colspan": "4", "rowspan": "1"},
        {"title": "22222222", "field": "", "colspan": "4", "rowspan": "1"},
        {"title": "22222222", "field": "", "colspan": "4", "rowspan": "1"},
        {"title": "22222222", "field": "", "colspan": "4", "rowspan": "1"}
    ],
    [
        {"title": "3333333333", "field": "", "colspan": "2", "rowspan": "1"},
        {"title": "3333333333", "field": "", "colspan": "2", "rowspan": "1"},
        {"title": " 4444444444", "field": "", "colspan": "3", "rowspan": "1"},
        {"title": " 4444444444", "field": "", "colspan": "3", "rowspan": "1"},
        {"title": " 4444444444", "field": "", "colspan": "3", "rowspan": "1"},
        {"title": "2", "field": "2", "colspan": "1", "rowspan": "3"}
    ],
    [
        {"title": "1", "field": "", "colspan": "1", "rowspan": "2"},
        {"title": "3", "field": "3", "colspan": "1", "rowspan": "2"},
        {"title": "4", "field": "4", "colspan": "1", "rowspan": "2"},
        {"title": "555555555", "field": "", "colspan": "2", "rowspan": "1"},
        {"title": "555555555", "field": "", "colspan": "2", "rowspan": "1"},
        {"title": "2", "field": "2", "colspan": "1", "rowspan": "3"}
    ],
    [
        {"title": "1", "field": "", "colspan": "1", "rowspan": "2"},
        {"title": "3", "field": "3", "colspan": "1", "rowspan": "2"},
        {"title": "4", "field": "4", "colspan": "1", "rowspan": "2"},
        {"title": "5", "field": "5", "colspan": "1", "rowspan": "1"},
        {"title": "6", "field": "6", "colspan": "1", "rowspan": "1"},
        {"title": "2", "field": "2", "colspan": "1", "rowspan": "3"}
    ]
]

the result is going to be like this

[
    [
        {"title": "11111111111", "field": "", "colspan": "3", "rowspan": "1"},
        {"title": "22222222", "field": "", "colspan": "4", "rowspan": "1"}
    ],
    [
        {"title": "1", "field": "1", "colspan": "1", "rowspan": "3"},
        {"title": "3333333333", "field": "", "colspan": "2", "rowspan": "1"},
        {"title": " 4444444444", "field": "", "colspan": "3", "rowspan": "1"},
        {"title": "2", "field": "2", "colspan": "1", "rowspan": "3"}
    ],
    [
        {"title": "1", "field": "", "colspan": "1", "rowspan": "2"},
        {"title": "3", "field": "3", "colspan": "1", "rowspan": "2"},
        {"title": "4", "field": "4", "colspan": "1", "rowspan": "2"},
        {"title": "555555555", "field": "", "colspan": "2", "rowspan": "1"}
    ],
    [
        {"title": "5", "field": "5", "colspan": "1", "rowspan": "1"},
        {"title": "6", "field": "6", "colspan": "1", "rowspan": "1"}
    ]
]
Sep.09,2021

arr.map(items => {
  var result = []
  items.forEach(item => {
    if (!result.find(_item => JSON.stringify(item) === JSON.stringify(_item))) {
      result.push(item)
    }
  })
  return result
})

< H2 > define class < / H2 >

related fields

  • title
  • field
  • colspan
  • rowspan

override the equals method
make use of set storage in java

< H2 > Code < / H2 >
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class C {
    private String title;
    private String field;
    private String colspan;
    private String rowspan;

    public static void main(String[] args) {

        C c0 = new C("111", "", "", "");
        C c1 = new C("111", "", "", "");
        C c2 = new C("222", "", "", "");
        C c3 = new C("222", "", "", "");

        Set s = new HashSet();
        s.add(c0);
        s.add(c1);
        s.add(c2);
        s.add(c3);

        s.forEach(
                sa ->
                        System.out.println(sa)
        );

    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        C c = (C) o;
        return Objects.equals(title, c.title) &&
                Objects.equals(field, c.field) &&
                Objects.equals(colspan, c.colspan) &&
                Objects.equals(rowspan, c.rowspan);
    }

    @Override
    public int hashCode() {
        return Objects.hash(title, field, colspan, rowspan);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getColspan() {
        return colspan;
    }

    public void setColspan(String colspan) {
        this.colspan = colspan;
    }

    public String getRowspan() {
        return rowspan;
    }

    public void setRowspan(String rowspan) {
        this.rowspan = rowspan;
    }

    public C(String title, String field, String colspan, String rowspan) {
        this.title = title;
        this.field = field;
        this.colspan = colspan;
        this.rowspan = rowspan;
    }

    @Override
    public String toString() {
        return "C{" +
                "title="" + title + "\'' +
                ", field='" + field + '\'' +
                ", colspan='" + colspan + '\'' +
                ", rowspan='" + rowspan + '\'' +
                '}';
    }
}
< H2 > results < / H2 >
C{title="111", field='', colspan='', rowspan=''}
C{title="222", field='', colspan='', rowspan=''}
true
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eafaab-4803a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eafaab-4803a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?