Finding the only unique string in an array using JavaScript

In JavaScript, finding a unique string that appears only once in an array is a common programming challenge. This article demonstrates two effective approaches to solve this problem.

Problem Statement

Write a JavaScript function that takes an array of strings as input and returns the only unique string present in the array. A unique string is defined as a string that occurs only once in the given array. If there are no unique strings, the function should return null.

Sample Input:

const strings = ["apple", "banana", "orange", "banana", "kiwi", "kiwi", "apple"];

Sample Output:

orange

In the given sample input, the string "orange" occurs only once in the array, making it the unique string.

Method 1: Using Nested Loops (Naive Approach)

This approach uses two nested loops to compare each string with every other string in the array. For each string, we check if it has any duplicates. If no duplicates are found, we return that string as the unique one.

function findUniqueString(arr) {
   for (let i = 0; i < arr.length; i++) {
      let unique = true;
      for (let j = 0; j < arr.length; j++) {
         if (i !== j && arr[i] === arr[j]) {
            unique = false;
            break;
         }
      }
      if (unique) {
         return arr[i];
      }
   }
   return null; // If no unique string is found
}

// Example usage
const strings = ["apple", "banana", "apple", "banana", "orange"];
const uniqueString = findUniqueString(strings);
console.log(uniqueString);
orange

Time Complexity: O(n²) where n is the length of the array.

Method 2: Using HashMap (Recommended)

This approach is more efficient. We first count the occurrences of each string using an object (HashMap), then find the string with a count of 1.

function findUniqueString(arr) {
   const countMap = {};
   
   // Count occurrences of each string
   for (let i = 0; i < arr.length; i++) {
      const str = arr[i];
      countMap[str] = (countMap[str] || 0) + 1;
   }
   
   // Find string with count of 1
   for (const key in countMap) {
      if (countMap[key] === 1) {
         return key;
      }
   }
   return null; // If no unique string is found
}

// Example usage
const strings = ["apple", "banana", "apple", "banana", "orange"];
const uniqueString = findUniqueString(strings);
console.log(uniqueString);
console.log("Count map:", countMap); // See the counting process
orange

Time Complexity: O(n) where n is the length of the array.

Comparison

Method Time Complexity Space Complexity Best for
Nested Loops O(n²) O(1) Small arrays
HashMap O(n) O(n) Larger arrays (recommended)

Alternative Using Map

You can also use JavaScript's built-in Map for better performance with large datasets:

function findUniqueStringWithMap(arr) {
   const countMap = new Map();
   
   // Count occurrences
   for (const str of arr) {
      countMap.set(str, (countMap.get(str) || 0) + 1);
   }
   
   // Find unique string
   for (const [key, count] of countMap) {
      if (count === 1) {
         return key;
      }
   }
   return null;
}

const strings2 = ["cat", "dog", "cat", "bird", "dog"];
console.log(findUniqueStringWithMap(strings2));
bird

Conclusion

The HashMap approach is generally recommended due to its O(n) time complexity. Use the nested loop method only for very small arrays where memory usage is a critical concern.

Updated on: 2026-03-15T23:19:00+05:30

985 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements