Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- int start = 0;
- int[] numbers = new int[7] { 2,1,2,1,5,6,5};
- }
- int[] numbers = new [] { 2, 1, 2, 1, 5, 6, 5 };
- int toFind = 5;
- // all indexes of "5" {4, 6}
- int[] indexes = numbers
- .Select((v, i) => new {
- value = v,
- index = i
- })
- .Where(pair => pair.value == toFind)
- .Select(pair => pair.index)
- .ToArray();
- List<int> indexes = new List<int>();
- for (int i = 0; i < numbers.Length; i++)
- {
- if (numbers[i] == yourNumber)
- indexes.Add(i);
- }
- // The value you are looking for it's indices
- var value = 2;
- // A list in which you will store the indices.
- var indices = new List<int>();
- // Loop through the elements of your array
- for(i=0; i<numbers.Length; i++)
- {
- // Check if the current value is equal to the value you are looking for.
- if(numbers[i]==value)
- {
- // If so, then add it's index in the list of indices.
- indices.Add(i);
- }
- }
- var indices = numbers.Select((number, index) => new { number, index })
- .Where(x => x.number == value)
- .Select(x => x.index);
- var indexes = numbers
- .Select((x, idx) => new { x, idx })
- .Where(c => c.x == number)
- .Select(c => c.idx);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement