# Floyd’s Tortoise and Hare Algorithm — for beginners

I’ve been brushing up on my algorithms by focusing on Leetcode Medium problems — or trying to, at least. I came across the Find the Duplicate Number question and learned about the Floyd’s Tortoise and Hare solution. It was a bit challenging to grasp, but with the help of some very useful art and re-reading the code, I decided to share how and why this particular algorithm is so helpful (and time efficient!)

## First approach

My first thought was to iterate through this array, and use the JavaScript method `indexOf`. `IndexOf` returns the index of the element if it can be found. I thought if I could iterate through the array and `indexOf` does not return -1, it would mean the number is not unique and therefore return nums[i].

Spoiler alert: I did say this was my first approach! This approach did not work, and that’s because it returned the value at the nums array at the zeroth index. Since nums at the initial index (which is 0) is 1, it is technically true that it is not equal to -1, and so this code would return the first value that this is true.

## Second approach

I took a second look at the constraints: we can’t modify the array and should be using constant O(1) time. Finally, I gave in and checked the solutions. To my surprise, I saw a number of solutions use something called Floyd’s Tortoise and Hare Algorithm.

Even after taking a look at their solution and watching a YouTube walkthrough, I didn’t understand how this approach was optimal. That’s when I came across Alisa Bajramovic’s blog post on Dev. I found it helpful, especially the diagram, but when I went back to my code, I was still confused.

## The Basics

We start by instantiating two variables, `tort` and `hare`. `Tort` is assigned the value of the nums array at index 0, and `hare` is assigned the value of nums at tort, which is one ahead, so at index 1. While the value of `tort` does not equal the value of `hare`, `tort` is the value of the next element (which is where hare was previously), and `hare` is the value of nums at the third element, or rather two indices ahead.

`Tort` is then the value of 0.

While `tort` is not equal to `hare`, `tort` is assigned the value of nums at the tort index, and then `hare` is assigned the value of nums at the `hare` index.

At the end of the while loop, you’ll return the variable `hare`.

Using this algorithm, we begin with `tort` assigned the value of 1, and `hare` to the value of 3. Since 1 !== 3, `tort` is now the value of 3, and `hare` has the updated value of 2 since it is at index 3. Again, these are not equal. You’ll continue to go through the array until `tort` reaches the end of the array — and since `hare` is the same value at `tort`, we return `hare`, or the value of 2.

## Runtime

This algorithm has a runtime of O(N), with the main factor that impacts runtime being the length of the array.

I’ve focused on building lately, and the focus on algorithms has been a great change of pace. These algorithms challenged me to consider different ways to achieve the solution — and it’s been even more challenging to find the optimal one! I’ve linked Alisa’s blog below, as I found the explanation and diagram helpful.
{"ft_lang_label":"__label__en","ft_lang_prob":0.91256887,"math_prob":0.95023865,"size":3563,"snap":"2021-43-2021-49","text_gpt3_token_len":858,"char_repetition_ratio":0.15116605,"word_repetition_ratio":0.039097745,"special_character_ratio":0.23491439,"punctuation_ratio":0.09703504,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9606432,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-07T11:14:46Z\",\"WARC-Record-ID\":\"<urn:uuid:fdd7458b-68a1-43a7-8871-753a29d2e615>\",\"Content-Length\":\"140358\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ed94a84d-3441-46f0-bd88-2e27991d0391>\",\"WARC-Concurrent-To\":\"<urn:uuid:452e47f7-bafb-46f8-b1ef-a46ef84ef19d>\",\"WARC-IP-Address\":\"162.159.153.4\",\"WARC-Target-URI\":\"https://aimee-liang.medium.com/floyds-tortoise-and-hare-algorithm-for-beginners-69750edf8196?responsesOpen=true&source=user_profile---------7----------------------------\",\"WARC-Payload-Digest\":\"sha1:L45SC254BDL4ZDOYCC7NDWMLUGS3M7RG\",\"WARC-Block-Digest\":\"sha1:T5SBIBXMI3RPXQJ3JDIFKWVUSJRBWH2D\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363376.49_warc_CC-MAIN-20211207105847-20211207135847-00246.warc.gz\"}"}