• 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
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

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

Aniruddha Chaudhari/14211/0
CodePython

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 algorithms

Method 2: Using Hashing

A 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 duplicate.

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 an element from the actual array and the 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.

FAQ (MCQ) question:

Vijay is given a problem to solve. he is given an array of names and asked to find duplicates in the names. Vijay builds a hashtable with all the names and uses that to find duplicates. Which of the following statements are true?

(unless otherwise stated, assume that the hash-function and hash-table are working well and doing a good job.) pick all that apply in most cases,

  1. the program will run in o(n) time in most cases,
  2. the program will run in o(n log n) time in most cases,
  3. the program will run in o(n^2) time
  4. if the hash-function is a doing a bad job, and there are lots of collisions, the program will run in o(n log n) time
  5. if the hash-function is a doing a bad job, and there are lots of collisions, the program will run in o(n^2) time

Answer: 5

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 is 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. Validate Sudoku
  12. Sort Circular Rotated Array
  13. String Permutations
  14. Min Arrow to Burst Bubbles
  15. Min Cost to Paint All Houses [Amazon]
  16. HourGlass with Max Sum
  17. Max Profit by Buying/Selling Stocks
  18. Hailstone Sequence
  19. Reverse String without affecting Special Characters
  20. Secure Conversation by Encry/Decry
  21. Special Elements in Matrix
  22. Next Greater No with Same set of Digits
  23. Smallest Subarray with Sum Greater than Given Number
  24. Group Anagrams
  25. Find Duplicates in Array in O(n)
  26. Find Two Unique Numbers from Array in O(n)
  27. Number Patterns & Finding Smallest Number
  28. First Unique Element in a Stream
  29. Flip Equivalent Binary Trees [TeachMint]
  30. Minimum Cost of Merging Files [Amazon]
  31. Minimum Distance for Truck to Deliver Order [Amazon]
  32. Longest Sequence of URLs
  33. Order Task for Given Dependencies
  34. Design Music Player
  35. Multilevel Parking System Design
  36. Minimum Coins Required
  37. Max Sum Subarray
  38. Max Avg Sum of Two Subsequences
  39. Merge Overlapping Intervals
  40. Longest Balanced Substring
  41. Longest Path in a Weighted Tree
  42. Generate Balanced Parentheses
  43. PostOrder Traversal Without Recursion

© 2022 – 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