Finding unique values in JavaScript

Stephanos Theodotou
4 min readApr 4, 2020

--

Photo by Randy Fath on Unsplash

Today, iterating over long lists of data-points to find which one is unique has become a common business logic operation. Think of a full-stack application like a CRM (customer resource management), or an analytics app focusing on filtering and rendering insights about sales data. You can also think of an example closer to home like the filtering features in Microsoft Excel. If you ever had to analyse or compare structured data, you have probably already used the “Filter for Unique Values” or “Filter for Duplicates” functionality. While Excel may not be our typical JavaScript app, it at least shows how powerful filtering functions like these can prove to be for your next app.

If you need some more motivation to look into filtering functions, rest-assured that they are a common JS interview question. So, let’s dive right into how we could approach such a problem. There are a few and faster ways to approach this, but in this article, we will focus on how we can use JS objects to find a unique value within a dataset. If you are looking for a complete solution, scroll to the bottom.

Now let’s break this down. First, consider the following array:

const data =[1,2,4,5,6,6,7,0,8,9,7,9,9,2,0,1,5,6,7,2,9,3,11,11,12]

By the way, in this array, the unique number is 3 in case you couldn’t spot it. Next, in our filtering function we need to instantiate a new empty object using an object initialiser:

const findSingle = (arr) => {  let obj = {}}

Having done that, we can now program our algorithm logic. We will start iterating over each number in the array of numbers using the “for..of” loop:

const findSingle = (arr) => {  let obj = {}  for(let item of arr ){  }}

For each item we will check our recently created object and do the following:

a) We will add the item (i.e., the number from the array we are currently iterating on) as a key in our object using the obj[name of key] notation . b) We will also set its value to +=1 so that it gets incremented every time we see the same number again as we iterate over the array.

const findSingle = (arr) => {  let obj = {}  for(let item of arr ){    obj[item]+= 1  }}

c) We wrap this logic in an if statement to check whether the key has already been created in our object. Doing this may feel counterintuitive since our object is initially empty of any keys but bear with me, this is intentional. Essentially what we are saying here is that step a will only be implemented only when we see the same key more than once. Now this means that we still need to add each item as a key into the object as we iterate.

const findSingle = (arr) => {  let obj = {}  for(let item of arr ){
if(obj[item]{
obj[item]+= 1
}
}
}

d) Worry not, we can do this inside the else block of our if statement as shown below. What we are saying here is that If we haven’t seen this particular key before as we iterate over the array, we will set that item as a key in the object with an initial value of 1:

const findSingle = (arr) => {  let obj = {}  for(let item of arr ){
if(obj[item]{
obj[item]+= 1
}else{
obj[item] = 1
}
}
}

As we iterate over the array, every number in our array is inserted as a unique key into our object. Thanks to the if statement, every time we see the same number in the array, we don’t create a duplicate key, but simply increment it’s associated value by 1 instead. You can check this as it’s running by logging the entries in the object using Object.entries(obj). This will log all the key-value pairs created and updated in the object as we are iterating:

for(let item of arr ){  console.log(Object.entries(obj);  ...
}
// log output:[ [ ‘1’, 1 ], [ ‘2’, 1 ] ]
[ [ ‘1’, 1 ], [ ‘2’, 1 ], [ ‘4’, 1 ] ]
[ [ ‘1’, 1 ], [ ‘2’, 1 ], [ ‘4’, 1 ], [ ‘5’, 1 ] ]
[ [ ‘1’, 1 ], [ ‘2’, 1 ], [ ‘4’, 1 ], [ ‘5’, 1 ], [ ‘6’, 1 ] ]
[ [ ‘1’, 1 ], [ ‘2’, 1 ], [ ‘4’, 1 ], [ ‘5’, 1 ], [ ‘6’, 2 ] ]
...
// notice how as soon as we see 6 another time, the value of key '6' is incremented to 2.

So by the end of the loop, all we have to do is check for the key in our object with a value of 1. To do this, we will now iterate over the object itself using the for…in loop instead of the for…of loop we used earlier. While the for-of loop iterates over any generally “iterable” object - such as our numbers’ array - the for-in loop will instead iterate over properties that are ‘key-ed’ such as the key-value pairs in our object:

for(let item in obj){  if(obj[item] === 1){ 
return item
}
}

Finally, let’s put everything together:

--

--

Stephanos Theodotou
Stephanos Theodotou

Written by Stephanos Theodotou

I'm a web developer and product manager merging code with prose and writing about fascinating things I learn.

No responses yet