|
1 | | -package arrays; |
2 | | - |
3 | | -public class FIndDublicate { |
4 | | - // Implementing an array as a HashMap for finding duplicate elements |
5 | | - void printRepeating(int m[],int size) { |
6 | | - |
7 | | - int i; |
8 | | - System.out.print("The repeating elements are:"); |
9 | | - |
10 | | - for(i=0;i<size;i++) { |
11 | | - |
12 | | - // using Math.abs() function to find the duplicate element in HashMap. |
13 | | - |
14 | | - if(m[Math.abs(m[i])]>=0) m[Math.abs(m[i])] = -m[Math.abs(m[i])]; |
15 | | - else System.out.print(Math.abs(m[i]) + " "); |
16 | | - } |
17 | | - } |
18 | | - |
19 | | - //Driver Method |
20 | | - public static void main(String[] args) { |
21 | | - |
22 | | - FIndDublicate dublicate= new FIndDublicate(); // Making an object of FindDuplicate class |
23 | | - |
24 | | - int m[] = {1,2,3,1,3,6,6}; // Putting or declare values in or to array |
25 | | - int m_size = m.length; // variables which store size or length of array |
26 | | - |
27 | | - dublicate.printRepeating(m,m_size); // calling above function to pass value in it to print repeating elements |
28 | | - |
29 | | - } |
30 | | - |
31 | | -} |
32 | | - |
| 1 | +package arrays; |
| 2 | + |
| 3 | +public class FIndDublicate { |
| 4 | + // Implementing an array as a HashMap for finding duplicate elements |
| 5 | + void printRepeating(int m[],int size) { |
| 6 | + |
| 7 | + int i; |
| 8 | + System.out.print("The repeating elements are:"); |
| 9 | + |
| 10 | + for(i=0;i<size;i++) { |
| 11 | + |
| 12 | + // using Math.abs() function to find the duplicate element in HashMap. |
| 13 | + |
| 14 | + if(m[Math.abs(m[i])]>=0) m[Math.abs(m[i])] = -m[Math.abs(m[i])]; |
| 15 | + else System.out.print(Math.abs(m[i]) + " "); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + //Driver Method |
| 20 | + public static void main(String[] args) { |
| 21 | + |
| 22 | + FIndDublicate dublicate= new FIndDublicate(); // Making an object of FindDuplicate class |
| 23 | + |
| 24 | + int m[] = {1,2,3,1,3,6,6}; // Putting or declare values in or to array |
| 25 | + int m_size = m.length; // variables which store size or length of array |
| 26 | + |
| 27 | + dublicate.printRepeating(m,m_size); // calling above function to pass value in it to print repeating elements |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | +} |
| 32 | +
|
0 commit comments