Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

two sum python

def twoNumberSum(array, targetSum):
    # Save the number seen so far
    seen = set()
    # Traverse the array
    for n in array:
      	# Assume n is the first number
        n2 = targetSum - n  # Calcualte which is the other number needed
        seen.add(n)  # Keep track of all the seen numbers
        if n2 != n and n2 in seen:
            return [n, n2]  # Found it
            
	return []
 
PREVIOUS NEXT
Tagged: #sum #python
ADD COMMENT
Topic
Name
1+5 =