How to write an algorithm to find the shortest distance between two elements of an array in Swift

Today I was presented to this problem. Basically given a list of elements, I’d have to find the nearest distance between two elements of that list.

You can see an example of given list. Notice that we’re not talking about a set, which means that elements could possibly repeat.

Computer Engineer + iOS Engineer. I am interested in Swift, Kotlin, Firebase, Data Structures, ASO & On Solving Real World Problems.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Integration with Magento: Benefits and Pitfalls

Fixing Bugs and Refactoring Code

Gradually improving our code quality with Test::Perl::Critic::Progressive

Mesosphere DC/OS Masterclass: Tips and Tricks to Make Your Life Easier

Salary Prediction Application Using ML inside Docker Container

How to Set up a COVID-19 Workflow and Dashboard Using the Google Cloud Platform

Python 100 projects in 100 days — Learning Journal

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
Alcivanio (Swift)

Alcivanio (Swift)

Computer Engineer + iOS Engineer. I am interested in Swift, Kotlin, Firebase, Data Structures, ASO & On Solving Real World Problems.

More from Medium

Resize Sprite After Window Resize

Shortcut for cleaning up your local cocoapod repository

Swift 的 for, if…else, switch 可以命名

Everyone can code — Loop jumper