Created
May 6, 2022 10:47
-
-
Save ironpython2001/c1a70c5d7bba6b9fac6e114e2ded3d36 to your computer and use it in GitHub Desktop.
Array Split with reference element in c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Desc: Array Split | |
//Say you have an array (or list) of arbitrary integers. | |
//The data structure must be reordered so that all values less than a | |
//special reference value are placed on the left. All values greater than | |
//or equal to the reference value are placed on the right. | |
//The ordering within the subranges is not relevant and may vary. | |
//Input Reference element Sample result | |
//[4, 7, 1, 20] 9 [1, 4, 7, 9, 20] | |
//[3, 5, 2] 7 [2, 3, 5, 7] | |
//[2, 14, 10, 1, 11, 12, 3, 4] 7 [2, 1, 3, 4, 7, 14, 10, 11, 12] | |
//[3, 5, 7, 1, 11, 13, 17, 19] 11 [1, 3, 5, 7, 11, 11, 13, 17, 19] | |
var lst = new List<int> { 4, 7, 1, 20 }; | |
int refelt = 9; | |
var lesslst = lst.Where(x => x < refelt).ToList(); | |
lesslst.Add(refelt); | |
var grtlst = lst.Where(x => x > refelt).ToList(); | |
var res = lesslst.Concat(grtlst).ToList(); | |
res.ForEach(x => Console.WriteLine(x)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment