Skip to content

Instantly share code, notes, and snippets.

@KCWill
Last active February 21, 2020 17:48
Show Gist options
  • Save KCWill/8d173136bc9eadafd7cb133bcd89b328 to your computer and use it in GitHub Desktop.
Save KCWill/8d173136bc9eadafd7cb133bcd89b328 to your computer and use it in GitHub Desktop.

Problem Solving in Coding

Recently in class, we were given a never-before-seen problem to solve with a partner. We took systematic steps in order to solve the problem. We had 45 mintues to solve the following problem: take in two arrays full of numbers, merge them together, get rid of any duplicates, and put the elements in order. An example of the desired outputs are shown below.

nums5 = [1, 3, 5, 7, 9, 11, 12];
nums6 = [1, 2, 3, 4, 5, 10, 12];
mergeArrays(nums5, nums6);
// => [1, 2, 3, 4, 5, 7, 9, 10, 11, 12]

This post is to walk you through the steps my partner and I took to solve this problem.

!Spoiler alert! We didn't get all the way through the problem, but we learned a great deal!

Pseudocoding

We started with breaking down the problem as much as possible. We figured that we should first merge the two arrays. We had a few ideas on how to do that, but we didn't worry about syntax at that point. Then we figured we'd get rid of dupliates next, and then put everything in order.

The first puzzle in the problem to figure out was how to check for and get rid of duplicate numbers. We wanted to take each number in the array and check it against every other number. If there was a duplicate, then replace one of the extras with the word false. We figured we'd need to have two loops that are nested in eachother. The first for loop would be for getting the first number to be used as the number the rest of the numbers will be checked against. The second for loop would cycle through each element in the array to compare it with the first selected number. We could then cycle through the array and if the value at an address is false, then we would remove that value.

The second hurdle to this problem is sorting the numbers into numerical order. We could take each element in the array and compare it with its neighbor (i+1). If the left number is bigger, then switch the two numbers. Then move through to the next numbers.

We broke down the problem into three steps: merging the arrays, finding duplicates, and ordering. At each step, we talked through eactly how the computer would evaluate the arrays and modify them.

Implementation

We ended up running out of time once we got our two arrays merged (step 1). We were about to implement step 2 where we would find duplicates in the array by cycling each number through the array to compare:

var nums1 = [1, 2, 3, 4, 5];
var nums2 = [6, 7, 8, 9, 10];
function mergeArrays(nums1, nums2) {
  var mergedArray = [];
  mergedArray = `${nums1},${nums2}`;
  for(var i = 0; i < mergedArray.length; i++){
    for(var j = 0; j < mergedArray.length; j++) {
      
    }
  }
  return mergedArray;
}
mergeArrays(nums1, nums2);

Conclusions

Although we weren't able to fully implement every step in the problem, we learned a great deal about talking through problems before jumping in and coding. I feel like both my parter and I would be able to implement the entire code in an additional 30 minutes. What was more important was understanding what we were able to code rather than just typing before thinking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment