C Program
#include<stdio.h> int main() { int a[100], f[101]={0}, n; scanf("%d", &n); for(int i=0; i<n; i++) { scanf("%d", &a[i]); f[a[i]]++; } for(int i=0; i<101; i++) if(f[i] > 1) printf("%d ", i); }
C Output
Input: 8 1 2 3 2 1 4 5 3 Output: 1 2 3
C++ Program
#include<iostream> #include<map> using namespace std; int main() { int n, x; cin >> n; map<int,int> f; while(n--) cin >> x, f[x]++; for(auto i : f) if(i.second > 1) cout << i.first << " "; }
C++ Output
Input: 6 4 5 6 4 5 7 Output: 4 5
JAVA Program
import java.util.*; class D { public static void main(String[] a) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Map<Integer,Integer> f = new HashMap<>(); for(int i=0; i<n; i++) { int x = sc.nextInt(); f.put(x, f.getOrDefault(x, 0)+1); } for(int k : f.keySet()) if(f.get(k) > 1) System.out.print(k + " "); } }
JAVA Output
Input: 5 8 9 10 8 9 Output: 8 9
Python Program
a = list(map(int, input().split())) f = {} for i in a: f[i] = f.get(i, 0) + 1 print(*[k for k in f if f[k] > 1])
Python Output
Input: 1 2 3 4 5 1 3 Output: 1 3
In-Depth Learning – Entire Concept in Paragraphs
What Does "Find Duplicates" Mean?
Finding duplicates in an array is finding the elements that occur more than once. You don't have to count how many times they occur (although that comes in handy in some situations), just find out which elements occur more than once. This is a typical use-case of hashing, where you hash elements onto their frequency and eliminate those happening more than once.
This teaches you to:
Traverse arrays
Use frequency counters
Work with hash maps/dictionaries
How the Code Works
All versions of the code:
Read the array of integers.
Keep track of the frequency of each element using either:
An integer array (for small, positive integers)
A map/dictionary (for general cases)
Loop through the frequency map.
Print all elements where frequency > 1 (i.e., duplicates).
In C, we use an array f[101] assuming elements are from 0 to 100.
We utilize hash maps or dictionaries in Python, Java, and C++ to support dynamic range as well as negative values.
Example
Input:
[3, 4, 5, 3, 2, 4]
Processing:
3 → 2 times
4 → 2 times
5, 2 → 1 time
Output:
Copy
3 4
Real-Life Analogy
Suppose you are creating a guest list for a party and you notice that some names show up more than once. You need to determine which guests were listed multiple times. To follow the analogy, in computer programs, duplicates can happen in:
Customer databases
Sensor logs
Survey responses
Transaction IDs
Discovering duplicates ensures data integrity and prevents redundant entries.
Where and When Is It Used?
This issue is applied in:
Data cleaning and preprocessing
Database deduplication
Interview questions for firms like Google, Infosys, Wipro, TCS
Competitive programming
Backend systems, logging utilities, or analytics pipelines
This is also a stepping stone to more complex problems such as:
Find all elements that occur twice
Find duplicates without extra space
Find missing and repeating numbers
Time and Space Complexity
Approach Time Space
Brute Force O(n²) O(1)
Using Map/Array O(n) O(n)
In the solution provided, we employ O(n) time and O(n) space for storing frequencies.
Python-Specific Tip
For a supremely clean and one-liner solution using collections.Counter:
python
from collections import Counter
a = list(map(int, input().split()))
c = Counter(a)
print(*[k for k in c if c[k] > 1])
SEO-Optimized Natural Paragraph for Ranking
Want to find duplicates in an array using C, C++, Java, or Python? This tutorial provides the cleanest and shortest code examples to identify all repeating elements in a list. This common programming question is crucial for data validation, cleaning, and logic building. Knowing how to implement hash maps, dictionaries, or frequency arrays makes you capable of tackling bigger issues such as counting occurrences, fraud detection, and data preprocessing. These solutions are interview-ready, efficient, and beginner-friendly, giving you a good understanding of pattern recognition within arrays.
Social Plugin