user339160
user339160

Reputation:

How to remove duplicates form string array

I have an string array that contains json strings . Is it possible to identify and remove all the records which contains the same uid ?

{"object":"user","entry":[{"uid":"823602904340066","id":"823602904340066","time":1429276535,"changed_fields":["feed"]}]}

{"object":"user","entry":[{"uid":"10203227586595390","id":"10203227586595390","time":1429278537,"changed_fields":["feed"]}]}

{"object":"user","entry":[{"uid":"10203227586595390","id":"10203227586595390","time":1429278531,"changed_fields":["feed"]}]}

Upvotes: 0

Views: 829

Answers (1)

Parth Akbari
Parth Akbari

Reputation: 651

A single item would always be unique, duplication occurs in multiple items. So, first of all convert the data into a list

// convert a list, add values
List<myobject> array = JsonConvert.DeserializeObject<list><myobject>>(json);
// get the distinct items..
// use the .ToList() to convert it back to a list.
array = array.Distinct().ToList();

This above code will first convert the json into list of objects. Then it will select only distinct items after that it will save that non duplicate list to the actual list. You can add a condition to the Distinct method using a Lambda expression.

Check this list for Ref

Upvotes: 1

Related Questions