I have a method that takes in two arrays of integers as parameters. I want to dynamically create objects based on the input. So if user puts in [2,3] and [4,3,2], I want to create 3 new Objects ( Friend2, Friend3 , Friend4). I have 2 issues I am stuck with:
Iterating through two lists and selecting only distinct values of both lists.
static int maxTokens( int[] friends_from, int[] friends_to){ **-- Need Code Here to iterate through both arrays and create distinct objects** foreach (var Friend in friends_from and friends_To) { Friend intFriend[i] = new Friend(); } }
Is there a way to use Linq to (If not what is the best way to accomplish this?):
Save myself from iterating through two lists adding both of them to a third list and selecting distinct
dynamically create object with distinct Name based on listItem
You can use union operation to get an array of distinct values like below and then create your object as required
static int maxTokens( int[] friends_from, int[] friends_to)
{
var uniqueFriends = friends_from.Union(friends_to).ToArray();
foreach (var Friend in uniqueFriends)
{
// add your logic for creating instance
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.