-
Notifications
You must be signed in to change notification settings - Fork 7
/
FindArray.cs
34 lines (27 loc) · 1.28 KB
/
FindArray.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
https://www.codewars.com/kata/59a2a3ba5eb5d4e609000055/csharp
7 kyu
Find array
You are given two arrays arr1 and arr2, where arr2 always contains integers.
Write the function find_array(arr1, arr2) such that:
For arr1 = ['a', 'a', 'a', 'a', 'a'], arr2 = [2, 4] find_array returns ['a', 'a']
For arr1 = [0, 1, 5, 2, 1, 8, 9, 1, 5], arr2 = [1, 4, 7] find_array returns [1, 1, 1]
For arr1 = [0, 3, 4], arr2 = [2, 6] find_array returns [4]
For arr1=["a","b","c","d"] , arr2=[2,2,2], find_array returns ["c","c","c"]
For arr1=["a","b","c","d"], arr2=[3,0,2] find_array returns ["d","a","c"]
If either arr1 or arr2 is empty, you should return an empty arr (empty list in python, empty vector in c++). Note for c++ use std::vector arr1, arr2.
*/
using System.Linq;
namespace CodeWars
{
public class KataFindArray
{
public static object[] FindArray(object[] arr1, int[] arr2)
{
//return arr2.Select(x => arr1.Length > x ? arr1[x] : null).Where(x => x != null).ToArray();
//return arr1.Length >= arr2.Length ? arr2.Select(x => arr1[x]).ToArray() : new object[0];
//return arr2.Where(x => x < arr1.Length).Select(x => arr1[x]).ToArray();
return arr2.Select(arr1.ElementAtOrDefault).Where(o => !(o is null)).ToArray();
}
}
}