BackmediumArraysFlipkart

Array Element Uniqueness Checker Solution

Problem Statement

Given an integer array nums, determine whether every value occurs exactly once. Return true if the array contains no duplicate elements; otherwise return false. The function receives the array as its sole argument and must produce a single boolean result.

Example 1
Input
[3,1,4,2]
Output
true

Explanation: The array contains the values 3, 1, 4, 2. Each value appears once, so the result is true.

Example 2
Input
[5,2,5,7]
Output
false

Explanation: The value 5 appears at indices 0 and 2, creating a duplicate. Hence the array is not unique and the result is false.

Example 3
Input
[10]
Output
true

Explanation: A single‑element array cannot have duplicates; therefore the result is true.

Constraints

  • 1 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • Expected time complexity: O(n)
  • Expected auxiliary space: O(n) or O(1) if modification of the input is allowed
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Array Element Uniqueness Checker — Problem Statement & Solution Guide

ArraysMedium
TimeO(n)
|
SpaceO(n)

Problem Description

Given an integer array nums, determine whether every value occurs exactly once. Return true if the array contains no duplicate elements; otherwise return false. The function receives the array as its sole argument and must produce a single boolean result.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Array Element Uniqueness Checker"

medium

WHY DOES IT MATTER?

Detecting duplicates is a fundamental building block for data validation, caching, and ensuring idempotent operations, making it a recurring pattern in system design and algorithmic interviews.

OPTIMIZATION CHALLENGE

The insight is that you don't need to compare every pair—maintaining a constant‑time membership structure (hash set) lets you detect a repeat the moment it appears, collapsing O(n²) to O(n).

REAL-WORLD CONNECTION

Think of a distributed ledger where each transaction ID must be unique; a duplicate ID indicates a replay attack, so fast uniqueness checks are crucial for security and consistency.

During an interview, insert each element into a set and immediately return false on a collision; this early‑exit strategy saves time and demonstrates proactive thinking.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(n)

Core Theory — Why This Approach?

The problem reduces to checking the injectivity of a mapping from indices to values, which is equivalent to detecting duplicates in a multiset. A naïve double‑loop comparison runs in O(n²) time and quickly becomes infeasible for large n because each element is compared against every other element, leading to quadratic blow‑up. The optimal paradigm leverages a hash‑based set or a sorting step: a hash set provides O(1) average‑case insertion and lookup, allowing us to scan the array once and flag any repeat, achieving linear time. Sorting transforms the problem into a linear scan of adjacent elements, but incurs O(n log n) time; the hash‑set approach is therefore preferred when both time and simplicity matter.

Interview Questions on This Problem

Q1How would you modify the solution if the array could contain integers outside the 32‑bit range, and you needed O(1) extra space?

You would sort the array in‑place (e.g., quicksort or heapsort) and then scan for adjacent equal values; sorting uses O(1) auxiliary space and runs in O(n log n) time, satisfying the space constraint.

Q2At a fintech firm, why might you prefer a Bloom filter over a hash set for duplicate detection on a massive streaming dataset?

A Bloom filter offers sub‑linear memory usage with a controllable false‑positive rate, enabling approximate duplicate detection when exactness is less critical and the data volume exceeds RAM capacity.

Q3In a high‑growth startup, how can you guarantee thread‑safe duplicate checks when multiple services concurrently insert into a shared data store?

Use a concurrent hash set or a database unique constraint; alternatively, employ atomic compare‑and‑swap operations or distributed locks to ensure only one thread can insert a given value.

Examples

Example 1

Input

[3,1,4,2]

Output

true

Explanation: The array contains the values 3, 1, 4, 2. Each value appears once, so the result is true.

Example 2

Input

[5,2,5,7]

Output

false

Explanation: The value 5 appears at indices 0 and 2, creating a duplicate. Hence the array is not unique and the result is false.

Example 3

Input

[10]

Output

true

Explanation: A single‑element array cannot have duplicates; therefore the result is true.

Constraints

  • 1 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • Expected time complexity: O(n)
  • Expected auxiliary space: O(n) or O(1) if modification of the input is allowed

Optimal Approach & Strategy

Traverse the array once, inserting each element into a hash set; if an insertion finds the element already present, return false immediately, else return true after the loop.

Brute Force Approach

Compare every element with every other element using two nested loops; if any pair matches, return false, otherwise true after all comparisons.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function hasAllUnique(nums){
    const set=new Set();
    for(const x of nums){
        if(set.has(x)) return false;
        set.add(x);
    }
    return true;
}

const fs=require('fs');
const data=fs.readFileSync(0,'utf8').trim().split(/\s+/).map(Number);
if(data.length===0){process.exit(0);} 
const n=data[0];
const nums=data.slice(1,n+1);
console.log(hasAllUnique(nums)?"true":"false");

Asked in Top Tech Interviews

Flipkart

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.