1) Given an array of numbers, write a function that returns a sequence (list or array) of each unique pair of elements (regardless of order) in the array.
2) What is the time complexity of your solution?
For example: given [0, 2, 6, 9], would return [[0, 2], [0, 6], [0, 9], [2, 6], [2, 9], [6, 9]].
My Solution
The code:
// var array = [0,2,6,9]; function uniq(array){ var n = array.length; var i,j; for (i = 0; i < n; i++){ for (j = i + 1; j < n; j++){ var output = array[i] + ", " + array[j] + ""; document.write(output); } } } uniq([0,2,6,9]);
Output:
0, 2 0, 6 0, 9 2, 6 2, 9 6, 9