Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. int start = 0;
  6. int[] numbers = new int[7] { 2,1,2,1,5,6,5};
  7. }
  8.  
  9. int[] numbers = new [] { 2, 1, 2, 1, 5, 6, 5 };
  10. int toFind = 5;
  11.  
  12. // all indexes of "5" {4, 6}
  13. int[] indexes = numbers
  14. .Select((v, i) => new {
  15. value = v,
  16. index = i
  17. })
  18. .Where(pair => pair.value == toFind)
  19. .Select(pair => pair.index)
  20. .ToArray();
  21.  
  22. List<int> indexes = new List<int>();
  23. for (int i = 0; i < numbers.Length; i++)
  24. {
  25. if (numbers[i] == yourNumber)
  26. indexes.Add(i);
  27. }
  28.  
  29. // The value you are looking for it's indices
  30. var value = 2;
  31.  
  32. // A list in which you will store the indices.
  33. var indices = new List<int>();
  34.  
  35. // Loop through the elements of your array
  36. for(i=0; i<numbers.Length; i++)
  37. {
  38. // Check if the current value is equal to the value you are looking for.
  39. if(numbers[i]==value)
  40. {
  41. // If so, then add it's index in the list of indices.
  42. indices.Add(i);
  43. }
  44. }
  45.  
  46. var indices = numbers.Select((number, index) => new { number, index })
  47. .Where(x => x.number == value)
  48. .Select(x => x.index);
  49.  
  50. var indexes = numbers
  51. .Select((x, idx) => new { x, idx })
  52. .Where(c => c.x == number)
  53. .Select(c => c.idx);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement