-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveMostRepeatedElements.java
More file actions
70 lines (64 loc) · 1.28 KB
/
RemoveMostRepeatedElements.java
File metadata and controls
70 lines (64 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package array_Programming.medium.day_10;
import java.util.Arrays;
import java.util.Scanner;
//12. Remove the most repeated elements from an array.
public class RemoveMostRepeatedElements
{
public static void removeMostRepeated(int [] arr)
{
int n = arr.length;
boolean [] visited = new boolean[n];
int maxFreq = 0;
for(int i = 0;i<n;i++)
{
if(visited[i])
{
continue;
}
int count = 1;
for(int j = i+1;j<n;j++)
{
if(arr[i] == arr[j])
{
count++;
visited[j] = true;
}
}
if(count > maxFreq)
{
maxFreq = count;
}
}
System.out.println("Array after removing most repeated elements :");
for(int i = 0;i<n;i++)
{
int count = 0;
for(int j = 0;j<n;j++)
{
if(arr[i] == arr[j])
{
count++;
}
}
if(count != maxFreq)
{
System.out.println(arr[i] + " ");
}
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of the array :");
int size = sc.nextInt();
int [] arr = new int[size];
System.out.println("Enter "+size + " elements : ");
for(int i = 0;i<arr.length;i++)
{
arr[i] = sc.nextInt();
}
System.out.println("Original Array : " + Arrays.toString(arr));
removeMostRepeated(arr);
sc.close();
}
}