Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use SkipWhile() to skip duplicate elements in two arrays?

I am trying to get a list of numbers (upto 9) that don’t contain any numbers already in a 3×3 cell from which is contained within a 81 large 2d array of integers. I’m attempting to make sudoku. There are likely better solutions that have nothing to with skipwhile or something similar, but they all seem to rely on duplicate numbers having the same index in both array which doesn’t work.

int[] inCellNumbers = thisCell.Intersect(Numbers).ToArray();
int[] availableNumbers = Numbers.SkipWhile(Numbers.Contains<int>(inCellNumbers) == true);

This is the code that I tried, numbers is an array of integers and I get this error:

‘MemoryExtensions.Contains(ReadOnlySpan, int)’ requires a receiver of type ‘ReadOnlySpan’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I was attempting to skip all numbers that are in ‘inCellNumbers’ and have them in ‘availableNumbers’ from ‘Numbers’

>Solution :

You can use the Enumerable.Except Method:

int[] availableNumbers = Numbers.Except(inCellNumbers).ToArray();

The Enumerable.SkipWhile Method stops skipping at the first element that does not fulfill the condition. Even if others follow later that do fulfill it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading