• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools

[Solved] Find Duplicate in Array in O(n) Linear Time

Aniruddha Chaudhari/10050/0
CodePython
YouTube CSEstack

Problem Statement:

An array contains n numbers ranging from 0 to n-1. There are some numbers duplicated in the array.

It is not clear how many numbers are duplicated or how many times a number gets duplicated.

How do you find a duplicated number in the array?

Example:

If an array of length 7 contains the numbers {2,  3, 1, 0, 2, 5, 3}, the implemented function (or method) should return either 2 or 3.

Method 1: Using Sorting

The simple solution to the above problem is sorting elements in the array list. If the number is the same as the number located next to it in the array, then the number is duplicate.

Python Program:

def findDup(liArr):
    liArr.sort()
    
    liDuplicate=[]
    for i in range(0, len(liArr)-1):
        if liArr[i]==liArr[i+1]:
            liDuplicate.append(liArr[i])

    return liDuplicate

print(findDup([2, 3, 1, 0, 2, 5,3]))

Output:

[2, 3]

Complexity:

In the best case, the merge sort takes time O(nlogn) to sort the n elements. After sorting, we are traversing over the sorted array again, this will take time O(n).

So the total complexity of this algorithm is O(nlogn+n) i.e. O(nlogn).

Let’s see another optimized solution which is having less complexity.

Read: different types of sorting algorithm

Method 2: Using Hashing

Hash table of size n is used. There will be one hash table entry for each element. The value in the hash table can be either 0 or 1.

Algorithm:

  • Take the hash table of size n (says hashIndex) and initialize each value in the hashtable to zero.
  • Traverse over each element in the array.
  • For each element (i) in the array
    • if hashIndex[i]==0, set hashIndex[i]=1
    • if hashIndex[i]==1, element is duplcate.

Let’s implement this logic by coding.

Python Program:

def findDuplicate(arr):
    liDuplicate=[]
    hashIndex=[0]*len(arr)
    for i in arr:
        if hashIndex[i]==0:
            hashIndex[i]=1
        elif hashIndex[i]==1:
            liDuplicate.append(i)
    
    return liDuplicate
    
arr=[4, 5, 2, 1, 4, 6, 6]
print(findDuplicate(arr))

Output:

[4, 6]

Complexity:

Here we are using the hashing technique. The hashIndex is a kind of hash table where the key is element from the actual array and value is 0 or 1.

Each element in the array is visited at once. The time complexity of this algorithm is O(n).

This question to find duplicates in array was asked on the NVIDIA interview coding round. You can solve this problem in any programming language like Python, C/++ or Java.

Are You Ready for the Next Challenge?

You can use a similar technique to solve the below coding challenge.

  • Write a program to print all the unique numbers present in the array.
  • Write a program to find out the number of times each number present in the array.

To solve the above coding challenge, you just have to tweak some lines of code in the above programs.

If you find the optimized solution to these problems, share it with me by writing in the comment section. You are free to use any programming leagues like C/C++, Java or Python.

Happy Coding!

Python Interview Questions eBook

coding challengePython
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Why?

Why Competitive Programming Important?

Coding Challenges for Practice

  1. Count Common Factor
  2. Does it Divide
  3. Sum of Sub Arrays
  4. Pair of Desired Sum
  5. Remove Duplicate Char from String
  6. Sort String by Char Freq (Python)
  7. Sort String by Char Freq (Java)
  8. Split Array into Equal Sum Subarray
  9. Validate IP Address
  10. Validate PAN Card Number
  11. Sort Circular Rotated Array
  12. Min Arrow to Burst Bubbles
  13. HourGlass with Largest Sum
  14. Max Profit by Buying/Selling Stocks
  15. Hailstone Sequence
  16. Reverse String without affecting Special Characters
  17. Secure Conversation by Encry/Decry
  18. Special Elements in Matrix
  19. Next Greater No with Same set of Digits
  20. Smallest Subarray with Sum Greater than Given Number
  21. Group Anagrams
  22. Find Duplicates in Array in O(n)
  23. Find Two Unique Numbers from Array in O(n)
  24. Number Patterns & Finding Smallest Number
  25. Minimum Cost of Merging Files [Amazon]
  26. Minimum Distance for Truck to Deliver Order [Amazon]
  27. Minimum Coins Required
  28. Max Sum Subarray
  29. Merge Overlapping Intervals
  30. Longest Balanced Subarray
  31. Longest Path in a Weighted Tree
  32. Generate Balanced Parentheses
  33. PostOrder Traversal Without Recursion

© 2021 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard