iqfareez
iqfareez

Reputation: 887

Remove duplicate entries in a list

I have this data:

[
  { "name": "Eric", "origin": "Tristan da Cunha" },
  { "name": "Eric", "origin": "Tristan da Cunha" },
  { "name": "Adelaide", "origin": "Cayman Islands" }
]

There is a duplicated data, how do I remove it before converting to a list?

Upvotes: 0

Views: 242

Answers (3)

Muhammad Junaid
Muhammad Junaid

Reputation: 1

You can do simply using this

  var listwithDuplicates = listwithDuplicates.toSet().toList();
  print(listwithDuplicates);

Upvotes: 0

Aamil Silawat
Aamil Silawat

Reputation: 8229

Remove Item by it's name

 final ids = uniqueLedgerList.map((e) => e["name"]).toSet();
 uniqueLedgerList.retainWhere((x) => ids.remove(x["name"]));
                                            
 log("Remove Duplicate Items--->$uniqueLedgerList");

Upvotes: 0

iqfareez
iqfareez

Reputation: 887

First, make a class for the data, include the hashCode and operator overrides. toString override is optional but would be helpful:

class DataModel {
  String? name;
  String? origin;

  DataModel({this.name, this.origin});

  @override
  int get hashCode => '$name, $origin'.hashCode;

  // this will do the magic
  @override
  bool operator ==(Object other) {
    return other is DataModel &&
        name.toString() == other.name.toString() &&
        origin.toString() == other.origin.toString();
  }

  @override
  String toString() {
    return "{name: '$name', origin: '$origin'}";
  }
}

Then, anywhere in your Dart code:

import 'data_model.dart';

void main(List<String> arguments) {
  // notice that Eric is duplicated in this entry
  List<DataModel> filterData = [
    DataModel(name: 'Eric', origin: "Tristan da Cunha"),
    DataModel(name: 'Eric', origin: "Tristan da Cunha"),
    DataModel(name: 'Adelaide', origin: "Cayman Islands"),
  ];

  // toSet() will remove the redundancy, then convert it back to toList()
  var removedDuplicatedDatas = filterData.toSet().toList();
  print(removedDuplicatedDatas);
}

Output:

[{name: 'Eric', origin: 'Tristan da Cunha'}, {name: 'Adelaide', origin: 'Cayman Islands'}]

Upvotes: 1

Related Questions