Challenge Accepted
//Given a list of numbers and a target number X, find the pairs of numbers in the list that add to yield X //FIRST ATTEMPT: "THE LOOP" public class Pair { int First{get;set;} int Second{get;set;} public Equals(Pair x, Pair y) { if(x.First == y.First && x.Second == y.Second || x.Second == y.Second && x.Second == y.First) return true; else {return false;} } } public class Calculate { public List<Pair> FindPairs(List<int> list, int target) { List<Pair> found = new List<Pair>(); for(int i=0;i<list.Count;i++) { int current = list[i]; for(int j=i+1;j<list.Count;j++) { if(list[j] == list[i] - target) { Pair newItem = new Pair(){ First = current, Second = list[j] }; bool exists = false; foreach(var item in found) { if(item.Equals(newItem)) { exists = true; } } if(!exists) { found.Add(newItem); } } } } } return found; } } //SECOND ATTEMPT: LOOK IN THE DICTIONARY AND TAKE THE HINT //Inspired by: http://www.kirupa.com/forum/showthread.php?237560-Find-Two-Numbers-in-an-Array-that-Sum-to-a-Particular-Value public class Pair { int First{get;set;} int Second{get;set;} public Equals(Pair x, Pair y) { if(x.First == y.First && x.Second == y.Second || x.Second == y.Second && x.Second == y.First) return true; else {return false;} } } public class Calculate { public List<Pair> FindPairs(List<int> list, int target) { List<Pair> found = new List<Pair>(); Dictionary<int,int> find = new Dictionary<in,int>(); for(int i=0;i<list.Count;i++) { if(find.Key(list[i]) { Pair newItem = new Pair(){ First = list[i], Second = find.Value(list[i]) }; bool exists = false; foreach(item in found) { if(item.Equals(newItem)) { exists = true; } } if(!exists) { found.Add(newItem); } } else { find.Add(target - list[i], list[i]); } } return found; } }