Get array items by index - c#

I have two arrays, one with values an one with indices
int[] items = { 1, 2, 3, 7, 8, 9, 13, 16, 19, 23, 25, 26, 29, 31, 35, 36, 39, 45 };
int[] indices = { 1, 3, 5, 6, 7, 9 };
now I want a result array from the items selected by the indices of indices array
// 2, 7, 9, 13, 19
int[] result = new []{ items[1], items[3], items[5], items[6], items[7], items[9] };
Question: Is there a more generic approach for this?

var results = Array.ConvertAll(indices, i => items[i]);

Try using Linq:
int[] items = { 1, 2, 3, 7, 8, 9, 13, 16, 19, 23, 25, 26, 29, 31, 35, 36, 39, 45 };
int[] indices = { 1, 3, 5, 6, 7, 9 };
int[] result = indices
.Select(index => items[index])
.ToArray();

A good old for loop should be able to do this job as well:
int[] items = { 1, 2, 3, 7, 8, 9, 13, 16, 19, 23, 25, 26, 29, 31, 35, 36, 39, 45 };
int[] indices = { 1, 3, 5, 6, 7, 9 };
List<int> resultList = new List<int>();
for (int i = 0; i < indices.Length; i++)
{
resultList .Add(items[indices[i]]);
}
Explanation:
when using the [ ] operator to access a specific index in indices it will return the number. This can again be used to index/access a specific location in items. So you have a double indexing.
EDIT:
If you need the result as an array you can use the ToArray method to convert it:
int [] result = resultList.ToArray();

For the sake of alternative:
int[] result = items.Select((value, index) => new { Index = index, Value = value }) //Add indexes
.Where(w => indices.Contains(w.Index)) //Filter by indexes
.Select(s => s.Value).ToArray(); //Extract values to result array

Related

Build pairs out of list - without repetition

to do it in linqI have an integer List and want to group these to a list of integer pairs.
var input = new[] {1, 24, 3, 2, 26, 11, 18, 13};
result should be: {{1, 24}, {3, 2}, {26, 11}, {18, 13}}
I tried:
List<int> src = new List<int> { 1, 24, 3, 2, 26, 11, 18, 13 };
var agr = src.Select((n, i) => new Tuple<int, int>(i++ % 2, n))
.GroupBy(t => t.Item1)
.ToList();
var wanted = agr[0].Zip(agr[1], (d, s) => new Tuple<int, int>(d.Item2, s.Item2));
Is there a better way to do it in linq?
Of course I can do it with a simple for-loop.
Edit:
I think I give MoreLinq a try. I also mark this as the answer even if it's an extension and not pure linq.
By the way - I think doing it with a for-loop is much more understandable.
You can use MoreLINQ Batch to split your input into a list of "length 2" lists. Or any other length you want.
List<int> src = new List<int> { 1, 24, 3, 2, 26, 11, 18, 13 };
List<IEnumerable<int>> wanted = src.Batch(2).ToList();
No need for MoreLINQ; Enumerate even- and odd-indexed values, and Zip
int[] input = new int[8] { 1, 24, 3, 2, 26, 11, 18, 13 };
var evenPositioned = input.Where((o, i) => i % 2 == 0);
var oddPositioned = input.Where((o, i) => i % 2 != 0);
var wanted = evenPositioned.Zip(oddPositioned, (even, odd) => new { even, odd }).ToList();
If you can garantee, that the length of the source can always be devided by 2:
List<int> src = new List<int> { 1, 24, 3, 2, 26, 11, 18, 13 };
var Tuple<int, int>[] wanted = new Tuple<int, int>[src.Count /2];
for(var i = 0; i < src.Count; i = i + 2)
wanted[i/2] = new Tuple<int, int>(src[i], src[i+1]);
a simple for loop is enough for this.Just start with 1 and increment it by 2
List<int> src = new List<int> { 1, 24, 3, 2, 26, 11, 18, 13 };
var list = new List<Tuple<int, int>>();
for(int i =1;i<src.Count;i=i+2)
{
list.Add(new Tuple<int, int>(src[i-1],src[i]));
}
In case of odd count last item will be skipped
Another simple loop solution featuring C# 7 tuples.
var input = new List<int> { 1, 24, 3, 2, 26, 11, 18, 13 };
var wanted = new List<(int, int)>();
for (var index = 0; index < input.Count; index += 2) {
wanted.Add((input[index], input[index + 1]));
}

Arrange Array in C# Issue

I've an array sequence 20,40,60,10,30,50. I want to sort this sequence into the following order 60,40,50,20,30,10 in C#.
Any Help? Thanks in advance☺
Very Simple if you have an Array
int[] arr = { 1, 2, 3, 5, 9, 0, 2, 10 };
arr.OrderBy(a => a);
arr.Reverse();
in case of list
List<int> abc = new List<int> { 1, 2, 3, 5, 9, 0, 2, 10 };
abc.Sort();
abc.Reverse();
Just use OrderByDescending of LINQ:
var list = new[] {20, 40, 60, 10, 30, 50};
var newList = list.OrderByDescending(x => x);
Console.WriteLine(string.Join(",", newList)); //60,50,40,30,20,10
You can try this
int[] array = new int[] { 20, 40, 60, 10, 30, 50 };
Array.Sort<int>(array,
new Comparison<int>((element1, element2) => element1.CompareTo(element2)));
to reverse sort
element2.CompareTo(element1)

Sort jagged array by second column using insertion sort C#

I have a jagged array in a c# program that is declared where column one represents a year, column two represents the number of a month (1-12) and column three represents some data for that month:
double[][] data = new double[3][]
{
new double[] {1930,1931,1931,1931,1931,1931,1931,1931,1931,1931,1931,1931,1931,1932,1932,1932,1932,1932,1932,1932,1932,1932,1932,1932,1932},
new double[] {12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
new double[] {5, 6, 8, 3, 5, 8, 9, 6, 5, 6, 7, 5, 3, 2, 2, 2, 5, 7, 8, 3, 2, 2, 1, 2, 5}
};
As you can see, the first array is ordered. I would like to know how I could sort the jagged array by the second column , in ascending order like this.
{1931,1932,1931,1932,1931,1932,1931,1932,1931,1932,1931,1932,1931,1932,1931,1932,1931,1932,1931,1932,1931,1932,1930,1931,1932}
{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12}
etc...
My question is, how would I be able to implement this using an insertion sort. It needs to be a custom algorithm and cannot make use of the Array.Sort algorithm that comes as part of C#
Thanks
The insertion sort algorithm can easily be generalized (abstracted) to work on indexes by defining two functions - one to compare two indexes and one to swap two indexes, like this:
public static class Algorithms
{
public static void InsertionSort(int start, int count, Func<int, int, int> compare, Action<int, int> swap)
{
for (int i = start + 1, end = start + count; i < end; i++)
for (int j = i; j > start && compare(j - 1, j) > 0; j--)
swap(j - 1, j);
}
}
Now you can achieve your goal by comparing second columns and swap all columns like this:
Algorithms.InsertionSort(0, data[1].Length,
(a, b) => data[1][a].CompareTo(data[1][b]),
(a, b) => { foreach (var col in data) Algorithms.Swap(ref col[a], ref col[b]); });
where Algorithms.Swap is another little helper:
public static void Swap<T>(ref T a, ref T b) { T c = a; a = b; b = c; }

List method to get difference of another list with duplicates (listobject.Expect method does not work)

There are two Lists. I need the difference
List<int> list1 = new List<int>() {18, 13, 22, 24, 20, 20, 27, 31, 25, 28 };
List<int> list2 = new List<int>() {18, 13, 22, 24, 20, 20, 20, 27, 31, 25, 28, 86, 78, 25 };
var listDif = list2.Except(list1);
foreach (var s in listDif)
Console.WriteLine(s);
Console.Read();
the answer should be 20, 86,78, 25
but it only outputs 86,78
If you want exactly that kind of behaviour you should try this:
List<int> list1 = new List<int>() { 18, 13, 22, 24, 20, 20, 27, 31, 25, 28 };
List<int> list2 = new List<int>() { 18, 13, 22, 24, 20, 20, 20, 27, 31, 25, 28, 86, 78, 25 };
// Remove elements of first list from second list
list1.ForEach(l => list2.Remove(l));
list2 = list2.Distinct().ToList();
list2.ForEach(d => Console.WriteLine(d));
Console.Read();
This works fine:
Make a clone of list2
Remove list1 items from list2
Code Sample:
List<int> list1 = new List<int>() { 18, 13, 22, 24, 20, 20, 27, 31, 25, 28 };
List<int> list2 = new List<int>() { 18, 13, 22, 24, 20, 20, 20, 27, 31, 25, 28, 86, 78, 25 };
var diff = list2;
list1.All(x => diff.Remove(x));
You can also perform Remove on list2, however, that will modify list2.
Because you only check which numbers in list1 are missing in list2
But you need to check which numbers in list2 doesn't exist in list1 and in
listDif.
You can do
List<int> diff = new List<int>();
foreach (int num in list1)
{
if (!list2.Contains(num))
{
diff.Add(num);
}
}
foreach (int num in list2)
{
if (!list1.Contains(num) && !diff.contains(num))
{
diff.Add(num);
}
}

How to group the same values in a sequence with LINQ?

I have a sequence. For example:
new [] { 10, 1, 1, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 }
Now I have to remove duplicated values without changing the overall order. For the sequence above:
new [] { 10, 1, 5, 25, 45, 40, 100, 1, 2, 3 }
How to do this with LINQ?
var list = new List<int> { 10, 1, 1, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 };
List<int> result = list.Where((x, index) =>
{
return index == 0 || x != list.ElementAt(index - 1) ? true : false;
}).ToList();
This returns what you want. Hope it helped.
var list = new List<int> { 10, 1, 1, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 };
var result = list.Where((item, index) => index == 0 || list[index - 1] != item);
It may be technically possible (though I don't think you can with a one-liner) to solve this with LINQ, but I think it's more elegant to write it yourself.
public static class ExtensionMethods
{
public static IEnumerable<T> PackGroups<T>(this IEnumerable<T> e)
{
T lastItem = default(T);
bool first = true;
foreach(T item in e)
{
if (!first && EqualityComparer<T>.Default.Equals(item, lastItem))
continue;
first = false;
yield return item;
lastItem = item;
}
}
}
You can use it like this:
int[] packed = myArray.PackGroups().ToArray();
It's unclear from the question what should be returned in the case of 1,1,2,3,3,1. Most answers given return 1,2,3, whereas mine returns 1,2,3,1.
You can use Contains and preserve order
List<int> newList = new List<int>();
foreach (int n in numbers)
if (newList.Count == 0 || newList.Last() != n)
newList.Add(n);
var newArray = newList.ToArray();
OUTPUT:
10, 1, 5, 25, 45, 40, 100, 1, 2, 3
Did you try Distinct?
var list = new [] { 10, 20, 20, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 };
list = list.Distinct();
Edit: Since you apparently only want to group items with the same values when consecutive, you could use the following:
var list = new[] { 10, 1, 1, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 };
List<int> result = new List<int>();
foreach (int item in list)
if (result.Any() == false || result.Last() != item)
result.Add(item);

Categories

Resources