forked from ashoklathwal/Code-for-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
N_Same.java
44 lines (39 loc) · 1000 Bytes
/
N_Same.java
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
/*
Given an array of 2n elements of which n elements are same and the remaining n elements are all different.
Write a program to find out the value which is present n times in the array.
There is no restriction on the elements in the array. They are random (In particular they not sequential)
Approach :
first traverse all element if adjacent element are equal print that element and return
otherwise cheak first 4 element.
*/
class N_same
{
public static void N_same_util(int[] arr, int m)
{
int n = m/2;
for(int i=1;i<m;i++)
{
if(arr[i-1] == arr[i])
{
System.out.println(arr[i]);
return;
}
}
if(arr[0] == arr[2] || arr[0] == arr[3])
{
System.out.println(arr[0]);
return;
}
if(arr[1] == arr[3])
{
System.out.println(arr[1]);
return;
}
}
public static void main(String[] args)
{
int[] arr = {2,3,4,5,6,1,1,1,1,1};
int m = 10;
N_same_util(arr, m);
}
}