← Accenture bank

Accenture · Question 4 · Arrays

Contains Duplicate

ArraysC++code⏱ O(n)💾 O(n)
Service-based

Problem

Check if any value appears at least twice in the array.

Approach

Use a hash set to detect duplicates.

Time O(n)Space O(n)

How it works

  1. 1Insert each number into a set.
  2. 2A repeat insert is a duplicate.

Sample input / output

Input:
nums = [1, 2, 3, 1]
Output:
true

Constraints

1 <= nums.length <= 10^5.

Solution — read it, then walk it

Time O(n)Space O(n)Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
STARTinclude <iostream>include <vector>include <unordered_set>using namespace stdboolcontainsDuplicate(vector<int>&nums)DEFINE main()vector<int> nums = {1, 2,3, 1}cout <<(containsDuplicate(nums) ?"true" : "false") << endlRETURN 0END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/10

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits