This repository was archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCount_Repeated_in_array.java
More file actions
139 lines (113 loc) · 3.34 KB
/
Copy pathCount_Repeated_in_array.java
File metadata and controls
139 lines (113 loc) · 3.34 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Aim: b) Write a program that takes as input a set of 15 numbers from the keyboard into an array of
// type int[] . Create another array that will also read 15 other numbers of type int into it. Now
// merge the elements of these two arrays into one. The output is to be a two-column list. The
// first column is a list of the distinct array elements; the second column is the count of the
// number of occurrences of each element.
package com.College_Java_Lab.Exp_5_Methods;
/*
For HackerRank :-
--------------
( LINK :- https://www.hackerrank.com/contests/2020csebjavalab/challenges/elements-count/problem )
import java.util.Scanner;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int [] arr1 = new int[15];
int [] arr2 = new int[15];
int [] arr = new int[30];
for ( int i = 0; i < 15; ++i ){
arr1[i] = input.nextInt();
}
for ( int i = 0; i < 15; ++i ){
arr2[i] = input.nextInt();
}
for( int i = 0; i < 15; ++i ){
arr[i] = arr1[i];
}
for( int i = 0; i < 15; ++i ){
arr[i+15] = arr2[i];
}
Arrays.sort(arr);
int temp = arr[0],count = 0;
for( int i : arr ){
if( temp != i ){
System.out.println(temp+" : "+count);
temp = i;
count = 1;
}
else{
++count;
}
}
System.out.println(temp+" : "+count);
}
}
*/
// For Record :-
// ----------
import java.util.Scanner;
import java.util.Arrays;
public class Count_Repeated_in_array {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int [] arr1 = new int[15];
int [] arr2 = new int[15];
for ( int i = 0; i < 15; ++i ){
arr1[i] = input.nextInt();
}
for ( int i = 0; i < 15; ++i ){
arr2[i] = input.nextInt();
}
int [][] ans = OccurrenceOfArrayElements( arr1, arr2 );
for ( int [] an : ans ) {
System.out.println(an[0] + " : " + an[1]);
}
}
public static int[][] OccurrenceOfArrayElements ( int[] arr1, int[] arr2 ) {
int [] arr = new int[30];
for( int i = 0, j = 0; i < 15; ++i, ++j ){
arr[j] = arr1[i];
arr[++j] = arr2[i];
}
Arrays.sort(arr);
int temp = arr[0],count = 0,j=0;
int [][] ans = new int [15][2];
for( int i : arr ){
if( temp != i ){
ans[j][0] = temp;
ans[j++][1] = count;
temp = i;
count = 1;
}
else{
++count;
}
}
ans[j][0] = temp;
ans[j][1] = count;
return ans;
}
}
// Input:
// -----
// 45 12 32 12 45 78 78 78 95 64 12 14 12 33 35
// 45 98 6 5 12 78 33 35 78 95 12 32 44 99 55
//
// Output:
// ------
// 5 : 1
// 6 : 1
// 12 : 6
// 14 : 1
// 32 : 2
// 33 : 2
// 35 : 2
// 44 : 1
// 45 : 3
// 55 : 1
// 64 : 1
// 78 : 5
// 95 : 2
// 98 : 1
// 99 : 1