Get indices of top x equal elements - c#

I need to get the indices of all the largest elements who are equal to each other in a sorted list of ints.
So given this list
elements: {1 , 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11, 12, 13, 13, 13}
index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
^ ^ ^
I will get this output
{16,17,18}
So far I've got
list.Select((x, i) => new {x, i})
To get the indices, but I can't use OrderBy() with First() or Single() because I need all the max elements' indices, not just the very top one.
Is there an elegant way to accomplish this (with LINQ or otherwise)?

Thus items are sorted, you only need to get index of first item with max value (that item will have exactly same value as last item), and then create range of indexes starting from this index to the end of list:
var items = new List<int> {1,1,2,3,4,4,5,6,7,7,8,9,10,11,11,12,13,13,13};
int startIndex = items.IndexOf(items[items.Count - 1]);
var indexes = Enumerable.Range(startIndex, items.Count - startIndex);

The easy/lazy way:
var a = new[] {1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11, 12, 13, 13, 13};
var b = new List<int>();
var max = a.Max();
for (var i = 0; i < a.Length; i++)
{
if (a[i] == max) b.Add(i);
}

I wouldn't use LINQ since it is a simple foreach over the collection.
//var data = new[] {1, 1, 13, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11, 12, 13, 13, 13};
var data = new[] {1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11, 12, 13, 13, 13};
var largest = int.MinValue;
var indices = new List<int>();
foreach (var x in data.Select((value, idx) => new {value, idx}))
{
if (x.value > largest)
{
indices.Clear();
largest = x.value;
}
// if unsorted
//if (x.value == largest) indices.Add(x.idx);
// if sorted you don't need to check against largest
indices.Add(x.idx);
}
Console.WriteLine("largest = {0}; indices = {1}", largest, string.Join(", ", indices));
Though if you must use LINQ, you can use this option instead of the foreach:
data.Select((value, idx) => new {value, idx})
.Aggregate(indices, (idxs, n) =>
{
if (n.value > largest)
{
idxs.Clear();
largest = n.value;
}
//unsorted
if (n.value == largest) idxs.Add(n.idx);
//sorted
//idxs.Add(n.idx);
return idxs;
});

This will get you a result of all the elements that have duplicates with their indices:
var result = elements.Select((value, index) => new { value, index })
.Where(g => elements.FindAll(v => v == g.value).Count > 1)
.GroupBy((a) => a.value).OrderByDescending((g) => g.Key).Take(3);
//I placed Take(3) as example since you said you need to find
//elements who are equal to each other,so only those that are
// not distinct(have duplicates) get into the collection.
//this will loop through the results and display the value(the item
//on the list) and its respective index.
foreach (var item in result.SelectMany(g => g))
{
string outcome = item.value + " - " + item.index;
Console.WriteLine(outcome);
}

Related

Custom sorting array - smaller values between larger values

I have an array A with values: {10, 12, 6, 14, 7} and I have an array B with values: {1, 8, 2}
I have sorted the array B in an ascending order and then combined both the arrays in a new array C as shown in the following code -
static void Main()
{
int A[] = {10, 12, 6, 14, 7};
int B[] = {1, 8, 2};
Array.Sort(B);
var myList = new List<int>();
myList.AddRange(A);
myList.AddRange(B);
int[] C = myList.ToArray();
//need values in this order: 10, 1, 12, 2, 8, 6, 14, 7
}
Now I wanna sort the array C this way: 10, 1, 12, 2, 8, 6, 14, 7
The smaller values should be between the larger values, for ex: 1 is between 10 and 12, 2 is between 12 and 8, 6 is between 8 and 14, so on and so forth.
How can I do this in C#?
If recursion is needed, how can I add it to the code?
What I understood from your example is that you are trying to alternate between large and small values such that the small value is always smaller than the number to the left and the right. I wrote an algorithm below to do that however it does not yield the exact same results you requested. However I believe it does meet the requirement.
The straggling 7 is considered the next smallest number in the sequence but there is no number that follows it. Based on your example it appears that is allowed.
To Invoke
int[] A = { 10, 12, 6, 14, 7 };
int[] B = { 1, 8, 2 };
var result = Sort(A, B);
Sort Method
public static int[] Sort(int[] A, int[] B)
{
var result = new int[A.Length + B.Length];
var resultIndex = 0;
Array.Sort(A);
Array.Sort(B);
//'Pointer' for lower index, higher index
var aLeft = 0;
var aRight = A.Length-1;
var bLeft = 0;
var bRight = B.Length - 1;
//When Items remain in both arrays
while (aRight >= aLeft && bRight >= bLeft)
{
//Add smallest
if (resultIndex % 2 > 0)
{
if (A[aLeft] < B[bLeft])
result[resultIndex++] = A[aLeft++];
else
result[resultIndex++] = B[bLeft++];
}
//Add largest
else
{
if (A[aRight] > B[bRight])
result[resultIndex++] = A[aRight--];
else
result[resultIndex++] = B[bRight--];
}
}
//When items only in array A
while (aRight >= aLeft)
{
//Add smallest
if (resultIndex % 2 > 0)
result[resultIndex++] = A[aLeft++];
//Add largest
else
result[resultIndex++] = A[aRight--];
}
//When items remain only in B
while (bRight >= bLeft)
{
//Add smallest
if (resultIndex % 2 > 0)
result[resultIndex++] = B[bLeft++];
//Add largest
else
result[resultIndex++] = B[bRight--];
}
return result;
}
Result
[14, 1, 12, 2, 10, 6, 8, 7]

Create a nested List of Objects from a single List

I have a list of items (not sure they are even or odd number of items). What I wanna do is, pick up records in the pair of 5 (which actually is a list), create another list and insert these pair of 5 lists into that new list.
Thanks
I can create a group of items by doing this
MyList
.Zip(Enumerable.Range(0, MyList.Count()),
(s, r) => new {
Group = r / 5,
Item = s })
.GroupBy(i => i.Group,
g => g.Item)
.ToList();
But I want to generate a nested list.
Not sure I understand your aim correctly, but you can try to use Dictionary for it:
MyList.Zip(Enumerable.Range(0, MyList.Count()),
(s, r) => new { Group = r / 5, Item = s })
.GroupBy(i => i.Group, g => g.Item)
.ToDictionary(g => g.Key, g => g.ToList());
It looks like you want to batch elements in batches of 5 items each. The MoreLinq package already offers the Batch operator for this:
var items=Enumerable.Range(0,17);
var batches=items.Batch(5);
foreach(var batch in batches)
{
Console.WriteLine(String.Join(" - ",batch));
}
This produces :
0 - 1 - 2 - 3 - 4
5 - 6 - 7 - 8 - 9
10 - 11 - 12 - 13 - 14
15 - 16
This is far faster than grouping as it only iterates the collection once.
MoreLINQ has other operators too, like Window, WindowLeft and WindowRight that produce sliding windows of values. items.Window(5) would produce :
0 - 1 - 2 - 3 - 4
1 - 2 - 3 - 4 - 5
...
11 - 12 - 13 - 14 - 15
12 - 13 - 14 - 15 - 16
The implementation
The operator's implementation is simple enough that you can just copy it into your project:
public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(this IEnumerable<TSource> source, int size)
{
return Batch(source, size, x => x);
}
public static IEnumerable<TResult> Batch<TSource, TResult>( IEnumerable<TSource> source, int size,
Func<IEnumerable<TSource>, TResult> resultSelector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (size <= 0) throw new ArgumentOutOfRangeException(nameof(size));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));
return _(); IEnumerable<TResult> _()
{
TSource[] bucket = null;
var count = 0;
foreach (var item in source)
{
if (bucket == null)
{
bucket = new TSource[size];
}
bucket[count++] = item;
// The bucket is fully buffered before it's yielded
if (count != size)
{
continue;
}
yield return resultSelector(bucket);
bucket = null;
count = 0;
}
// Return the last bucket with all remaining elements
if (bucket != null && count > 0)
{
Array.Resize(ref bucket, count);
yield return resultSelector(bucket);
}
}
}
The code uses arrays for efficiency. If you really want to use mutable lists you can change the type of bucket to a List<T>, eg :
if (bucket == null)
{
bucket = new List<TSource>(size); //IMPORTANT: set the capacity to avoid reallocations
}
bucket.Add(item);
...
Why not just GroupBy?
using System.Linq;
...
int groupSize = 5;
var result = MyList
.Select((item, index) => new {
item,
index
})
.GroupBy(pair => pair.index / groupSize,
pair => pair.item)
.Select(group => group.ToList())
.ToList();
If you have a collection of items
var items = Enumerable.Range(1, 20);
And you want to take, say, 5 at a time
var setSize = 5;
You can iterate over the collection by index, and take that 5 at a time as a list, and put all those lists of 5 into one outer list
Enumerable.Range(0, items.Count() - setSize).Select(x => items.Skip(x).Take(setSize).ToList()).ToList()
The result (from C# interactive shell) looks like
List<List<int>>(15) {
List<int>(5) { 1, 2, 3, 4, 5 },
List<int>(5) { 2, 3, 4, 5, 6 },
List<int>(5) { 3, 4, 5, 6, 7 },
List<int>(5) { 4, 5, 6, 7, 8 },
List<int>(5) { 5, 6, 7, 8, 9 },
List<int>(5) { 6, 7, 8, 9, 10 },
List<int>(5) { 7, 8, 9, 10, 11 },
List<int>(5) { 8, 9, 10, 11, 12 },
List<int>(5) { 9, 10, 11, 12, 13 },
List<int>(5) { 10, 11, 12, 13, 14 },
List<int>(5) { 11, 12, 13, 14, 15 },
List<int>(5) { 12, 13, 14, 15, 16 },
List<int>(5) { 13, 14, 15, 16, 17 },
List<int>(5) { 14, 15, 16, 17, 18 },
List<int>(5) { 15, 16, 17, 18, 19 }
}
If you want each item to only show up once in each list, you can alter the above. Let's assume there's an odd number of elements:
var items = Enumerable.Range(1, 11);
You want to change the initial range used to index into your collection. Instead of taking 5 at a time on each index, it will jump the index up by 5 each iteration. The only tricky part is making sure to handle when the collection divides the number of elements you want to take; you don't want to end up with an empty list at the end. That is, this is incorrect:
Enumerable.Range(0, items.Count() / setSize).Select( // don't do this
The statement is then
Enumerable.Range(0, ((items.Count() - 1) / setSize) + 1).Select(x => items.Skip(setSize * x).Take(setSize).ToList()).ToList();
The result (from C# interactive shell) looks like
List<List<int>>(3) {
List<int>(5) { 1, 2, 3, 4, 5 },
List<int>(5) { 6, 7, 8, 9, 10 },
List<int>(1) { 11 }
}

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]));
}

Aggregate sub-lists using LINQ

I have a list of objects (i.e. integers) and I want to aggregate sub-lists with LINQ.
For example:
Original list: [ 1, 4, 5, 3, 4, 10, 4, 12 ]
Sub-lists: [ [1,4,5,3], [4,5,3,4], [5,3,4,10], [3,4,10,4], [4,10,4,12] ]
Result (Aggregated List): [ 5, 5, 10, 10, 12 ]
I want to create the maximum of a sub-list for each element containing itself and the following n = 3 elements. Is this possible with LINQ or do I need to create my own aggregation mechanism?
Thanks in advance,Christian
public IEnumerable<IEnumerable<int>> GetSubLists(int[] collection)
{
for(int i = 0; i< collection.Length - 3; i++)
yield return collection.Skip(i).Take(4);
}
GetSubLists(original).Select(l => l.Max());
Or in one line
int[] original = {1, 4, 5, 3, 4, 10, 4, 12 };
int chunkCount = 4;
Enumerable.Range(0, original.Length - chunkCount + 1).Select(i => original.Skip(i).Take(chunkCount))
.Select(l => l.Max());
var result = sublists.Select(sl => sl.Max());
// [5,5,10,10,12]
Creating sub-lists:
List<int> original = new List<int> { 1, 4, 5, 3, 4, 10, 4, 12 };
int sublistSize = 4;
// check if original size is greater than required sublistSize
var sublists = Enumerable.Range(0, original.Count - sublistSize + 1)
.Select(i => original.GetRange(i, sublistSize));
// [[1,4,5,3],[4,5,3,4],[5,3,4,10],[3,4,10,4],[4,10,4,12]]
IEnumerable<int[]> GetLists (int[] list, int size )
{
return Enumerable.Range(0, list.Length - size + 1).Select(x => list.Skip(x).Take(size).ToArray());
}
Sample:
var list = new[] {1, 4, 5, 3, 4, 10, 4, 12};
var max = GetLists(list, 4).Select(x => x.Max()).ToArray();
The Sub-lists intermediate result can be constructed with a "Sliding Window" function.
The desired Result then is the function Max() mapped over the windows with Select().
var originalList = new [] {1, 4, 5, 3, 4, 10, 4, 12};
var sublists = originalList.Window(4); // [ [1,4,5,3], [4,5,3,4], ... ]
var result = sublists.Select(Enumerable.Max); // [ 5, 5, 10, 10, 12 ]
Efficient Window function:
public static IEnumerable<IEnumerable<T>> Window<T>(this IEnumerable<T> source,
int windowSize)
{
if(windowSize < 1) throw new ArgumentException("windowSize must positive", "windowSize");
var q = new Queue<T>(windowSize);
foreach(var e in source)
{
q.Enqueue(e);
if(q.Count < windowSize) continue; // queue not full yet
yield return q;
q.Dequeue();
}
}

Filter C# collection of float given a minimum difference value between adjacent elements

Suppose I have an ordered list of float (ascending).
I want to remove from it each element whose difference between next element and itself is less than a given threshold.
I need something like this:
List<float> orderedList;
IEnumerable<float> query = orderedList.Where(currentNum , nextNum => nextNum - currentNum < threshold);
Is that possible? If yes how?
Try this out:
var filteredElements = new List<float>();
float ? prev = null;
orderedList.ToList().ForEach((e)=>{
if (prev.HasValue)
{
if (e-prev >= threshold)
filteredElements.Add(prev.Value);
}
prev = e
});
Try this -
List<float> orderedList = new List<float>() { 12, 14, 34, 45 };
List<float> itemsToRemove = orderedList.Where((item, index) =>
index < orderedList.Count - 1 &&
orderedList[index + 1] - item < threshhold).ToList();
Try this instead :
var ls1 = ls.Where((item, index) =>
item - ls[ls.Count == index + 1 ? index : index + 1] >= 0);
Hope this will help !!
This appears to work. (Though there are some potentially misunderstood implications of your question.)
var input = new List<float>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 18, 21, 24, 27, 29, 35, 40, 46, 59 };
var output = input.Zip(input.Skip(1).Concat(new[]{float.MaxValue}), (a, b) => new { a, b }).Where(x => x.b - x.a > 2).Select(x => x.a);
This produces the output of:
15, 18, 21, 24, 29, 35, 40, 46, 59
This has the advantage of working with any IEnumerable.

Categories

Resources