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

August Radjoe
3 min readAug 20, 2019

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…

August Radjoe

Now: MS CS @ Boston U / Prev: Ignite Tournaments, DeFi Alliance, Persistence, Eth India Co