• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Check if an array has duplicate numbers in O(n) time and O(1) space

January 8, 2015 by Dhaval Dave

We have array  a[ ]= {1, 2, 3, 4, 5, 6, 7, 8, 4, 10};
And we need to find a number which is duplicate in O(n) and Space complexity O(1)

Method 1 ) Sorting
Sort it and u’ll get array = 1, 2, 3, 4, 4, 5, 6, 7, 8, 10 and find repeating element such that its index are different, but sorting takes O(nlogn)

Method 2) Hashing.

Keep Hashmap with <key, count> pair
Hashmap will contain
1,1
2,1
3,1
4,2
and return 4.
But it will take O(n) space Complexity

Method 3) Changing Array

Check if A [ abs(A[i]) ] is positive, then make it negative,
if its already negative then we found number.
( This trick will work only if all numbers are positive )

say array is 1, 2, 3, 4, 5, 6, 7, 8, 4, 10

so
while processing 1, w’ll make A[1] = -A[1], so 2 will become -2.
while processing 2, w’ll make A[2] = -A[2], so 3 will become -3.
while processing 3, w’ll make A[3] = -A[3], so 4 will become -4.
while processing 4, w’ll make A[4] = -A[4], so 5 will become -5.
while processing 5, w’ll make A[5] = -A[5], so 6 will become -6.
.
.
.
while processing 4, we checked that A[4] is already negative, So this is duplicate number.

-In order to get Original Array, we can make all sign as positive after wards
(Write/Append this code in ur code)


Working Code http://ideone.com/Y9eStG

#include <iostream>
using namespace std;

int main() {
int a[]= {1,2,3,4,5,6,7,8,4,10};
for(int i=0;i<sizeof(a)/sizeof(*a);i++){
if ( a[abs(a[i])]>0 ){
a[abs(a[i])]=-(a[abs(a[i])]);
}
else if (a[abs(a[i])]<0){
cout<<abs(a[i])<<endl;
}

}
for(int i=0;i<sizeof(a)/sizeof(*a);i++){
cout << a[i] <<endl;
}
return 0;
}

Duplicate
4

Array 
1
-2
-3
-4
-5
-6
-7
-8
-4
10

Similar Articles

Filed Under: Adobe Interview Questions, Interview Questions, problem Tagged With: Array

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Python Dictionaries

Number of Islands BFS/DFS

simple sql injection

DFS (Depth First Search)

Convert number to words java

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Implement LRU Cache

Stickler thief

Possible sizes of bus to carry n groups of friends

Implement a generic binary search algorithm for Integer Double String etc

Generic Object Oriented Stack with Template

Longest Increasing Subsequence

Sort an array according to the order defined by another array

Given Set of words or A String find whether chain is possible from these words or not

Facebook Interview Question : Interleave List

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Edit Distance ( Dynamic Programming )

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Closed Parentheses checker

Amazon Interview Experience – SDE Chennai

Printing intermediate Integers between one element & next element of array

Advanced SQL Injection

Best Java Book | Top Java Programming Book for Beginners

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

CodeChef Code SGARDEN

The Magic HackerEarth Nirvana solutions Hiring Challenge

Maximum size of square sub matrix with all 1’s in a binary matrix

Daughter’s Age VeryGood Puzzle

FizzBuzz Solution C C++

Copyright © 2025 · Genesis Framework · WordPress · Log in