Java: Functions, Loops, Variables, and a sock merchant

Introduction to the series, Structure of a Java program and stuff

First, I talk about basics, such as in the previous article. In this one, I am going to pick up a problem from HackerRank’s interview prep kit and solve it, explaining Functions, For loops and working with variables.

Over the next few articles, I will focus on all basics such as OOPs, Methods, Constructors, the ten I/O types in Java.

I will dedicate a few articles solely to advanced OOPs.

From there we will move to databases, networking, concurrency and then make a text editor and a breakout game.

All that said and done, let’s jump on to our first problem. But first, music recommendation for this post is a beautiful Synthwave mix I stumbled upon late one night in Mumbai.

Back to the problem:

Attached the image because the markdown from HackerRank wasn’t getting copied

Fairly simple problem, let me break it down.

A large pile of socks where each sock is represented by an integer stored in an array. Said Socks would have the same integer value if they are the same color. Find out the number of pairs of socks.

Fine, sounds like a good problem to me (Personal lifehack: Tie a pair of socks together so they don’t get mixed up.)

I’ll post the function first, and take everything from there.

First, let me explain how a function works in Java or any language.

Suppose you have to teach a robot how to pick up and object lying in front of it. How would you do it?
Of course, one method is to give the robot the exact details of picking up something. And then do it again when you need to pick something up. And again. And so on. Or you could write this into the robot’s memory and ask the robot to go refer to that memory every time you need to get something picked. Let’s say, that code you ask the robot to refer to is called pickUpStuff. So in code, we call it as pickUpStuff() and inside pick up stuff we write what needs to be done. Sometimes, a function returns a value, which means once you call it, it gives you a result. A function without a return gives a NoneType value as a result, which essentially has no value. A function can also have parameters, so say we say pickUpStuff(Object) but only pick up a pen, we pass Pen as the parameter since it is the object.

Now let’s see what pickUpStuff would look like as code;

pickUpStuff(object){

extendArm();

analyzeSituation();

goTo(object);

pickedup=grabObject();

return “Picked up” + pickedup

}

print ( pickUpStuff(pen) );

OUTPUT:

Picked up pen

Let’s go ahead with the algorithm (I have already written a somewhat messy, commented code)

  1. Set num_pairs to 0
  2. Define a Set called set. This is done by Set<Integer> [name of set]=new HashSet(); [As we go over OOPs, this is going to make sense. Also, we choose set because it removes duplicate values]
  3. Loop from and integer i=0 (You can declare integers as arguments) to ar.length, and increment ‘i’ by 1 every iteration.
  4. If ar[i], that is the element of the array passed in the argument, is not in the set, add it
  5. if ar[i] is in set, then increase num_pair by 1, and remove ar[i] value from set.
  6. return num_pair

This would give you the desired result and hopefully all your testcases pass.

In the next article, I will take another problem, talk arrays, classes, methods and more. Stay tuned.

Research Intern @ Persistence.one

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store