here i have code to print non repeated numbers from given array.I know the concept but while applying to logic i'm unable to get exact output.
public class NumberFinder {
public static void main(String[] args) {
int count = 0;
int[] input = {10, 2, 4, 2, 5, 1, 23, 34, 12};
int[] output = new int[input.length];
for (int i = 0; i < input.length; i++) {
for (int j = 1; j < input.length; j++) {
if (input[i] == input[j]) {
count++;
} else {
output[j - 1] = input[i];
System.out.println(input[j]);
}
}
}
}
}
from the above array i need to print non repeated values are as follows:10,4,5,1,23,34,12. Could anybody guide me.Your help will be appreciated.
Edit - Almas - This question is not duplicate of what Suresh has marked for.
Here's what you could do: You could use Map and maintain a count of times its repeating like:
int[] input = new int[] { 1, 1, 3, 7, 7, 8, 9, 9, 9, 10 };
boolean found;
for (int i = 0; i < input.length; i++) {
found = false;
for (int j = 0; j < input.length; j++) {
if (i == j)
continue;
if (input[i] == input[j]) {
found = true;
break;
}
}
if (!found) {
System.out.println(input[i]);
}
}
You can try also this code:
public static void main(String[] args)
{
int[] input = { 3,10,7, 10, 2, 4, 2, 5, 1, 23, 34, 5, 12, 4, 63,63,111 };
int duplicateNumber[] = new int[input.length];
int k = 0;
for (int i = 0; i < input.length; i++)
{
boolean alreadyDuplicated = false;
for (int l = 0; l < k; l++)
{
if (input[i] == input[duplicateNumber[l]])
{
alreadyDuplicated = true;
break;
}
}
if (!alreadyDuplicated)
{
boolean insertFirst = false;
for (int j = i + 1; j < input.length; j++)
{
if (input[i] == input[j])
{
if (insertFirst == false)
{
duplicateNumber[k++] = i;
insertFirst = true;
}
duplicateNumber[k++] = j;
}
}
}
}
int[] output = new int[input.length - k];
int out = 0;
for (int i = 0; i < input.length; i++)
{
boolean duplicatedFound = false;
for(int j = 0;j<k;j++)
{
if(input[i] == input[duplicateNumber[j]])
{
duplicatedFound = true;
}
}
if(!duplicatedFound)
{
output[out++] = input[i];
}
}
for (int l = 0; l < output.length; l++)
{
System.out.println(output[l]);
}
}