DEV Community

Cover image for Day 66 of 100 days dsa coding challenge
Manasi Patil
Manasi Patil

Posted on

Day 66 of 100 days dsa coding challenge

Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! πŸ’»πŸ”₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! πŸš€

100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney

Problem:

https://www.geeksforgeeks.org/problems/find-duplicates-in-an-array/1

Array Duplicates

Difficulty: Easy Accuracy: 18.95%

Given an array arr[] of size n, containing elements from the range 1 to n, and each element appears at most twice, return an array of all the integers that appears twice.
Note: You can return the elements in any order but the driver code will print them in sorted order.

Examples:
Input: arr[] = [2, 3, 1, 2, 3]
Output: [2, 3]
Explanation: 2 and 3 occur more than once in the given array.
Input: arr[] = [3, 1, 2]
Output: []
Explanation: There is no repeating element in the array, so the output is empty.
Constraints:
1 ≀ n ≀ 106
1 ≀ arr[i] ≀ n

Solution:
class Solution:
def findDuplicates(self, arr):
res = []
for i in range(len(arr)):
idx = abs(arr[i]) - 1
if arr[idx] < 0:
res.append(abs(arr[i]))
else:
arr[idx] = -arr[idx]
return res

Top comments (0)