Cio
Cio

Reputation: 1

How to print duplicated value only once each using only for loop and if in java

    int[] numbers = {3, 2, 5, 11, 7, 10, 11, 3, 15, 11, 17, 10, 5};
    int count = 0;
    boolean dup = false;

    System.out.println("arrays value");
    for (int n : numbers ) {
        System.out.print(n +" ");
    }
    
    System.out.println("\n\nDuplicated value on arrays: ");
    for (int a = 0 ; a < numbers.length ; a++ ) {
        for (int b = a + 1 ; b < numbers.length ; b++ ) {
            if (numbers[a] == numbers[b]) {
                count = numbers[a];
                dup = true;
            }
        }
        
        if (dup) {
            System.out.print(count +" ");
            dup = false;
            count = 0;
        }
    }

I want to print duplicated value only once each using only for loop and if

this output will print 3 5 11 10 11, how do I make only 3 5 11 10.

Upvotes: 0

Views: 1472

Answers (4)

MarkusAnd
MarkusAnd

Reputation: 812

For this it is smart to use the data structure set which is a collecion that do not allow duplicates. This means that whatever the number of same values you add to the set only one will be stored. Now you can simply write

Set<Integer> mySet = new HashSet<>(Arrays.asList(numbers));
for(int number : mySet) {
    System.out.println(number);
}

If you only want to print the values that are duplicates and not the values that only exist once (your question is not entirely clear) you may do something like this instead

Set<Integer> mySet = new HashSet<>();
for(int number : numbers) {
   if(!mySet.add(number)) { //the set method add(e) returns false if e is a duplicate i.e. can not be added to the set
      System.out.print(duplicate + " ");
   }
}

Upvotes: 4

vsfDawg
vsfDawg

Reputation: 1527

I assume that you are allowed multiple for-loops and the use of data structures.

Iterate over the values and accumulate the count the occurrences of each value in a Map. Then iterate over the Map and output the values with a count > 1.

public static void main(String[]args) {
  int[] values = {...the values...};
  Map<Integer,Integer> map = new HashMap<>();
  for (int value : values) {
    if (map.containsKey(value)) {
      map.put(value, 1 + map.get(value));
    } else {
      map.put(value, 1);
    }
  }
  for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
    if (entry.getValue() > 1) {
      System.out.printf("Value %d was duplicated", value);
    }
  }
}

Upvotes: 0

Krk Rama Krishna
Krk Rama Krishna

Reputation: 117

If you want with for loop

import java.util.ArrayList;

public class stackov {
    public static void main(String[] args) {
        int[] numbers = {3, 2, 5, 11, 7, 10, 11, 3, 15, 11, 17, 10, 5};
        int count = 0;
        boolean dup = false;

        System.out.println("arrays value");
        for (int n : numbers) {
            System.out.print(n + " ");
        }
        ArrayList<Integer> integers = new ArrayList<>();
        System.out.println("\n\nDuplicated value on arrays: ");
        for (int i : numbers) {
            if (!integers.contains(i)) {
                integers.add(i);
            }
        }

        for (int x : integers) {
            System.out.print(x + " ");
        }
    }

}

Upvotes: 0

AndreiXwe
AndreiXwe

Reputation: 763

You need to mark all values that have been printed out, using a boolean array (instead of just a single boolean). Try something like:

        System.out.println("\n\nDuplicated value on arrays: ");
        boolean[] dup = new boolean[1000];
        for (int a = 0 ; a < numbers.length ; a++ ) {
            if (dup[numbers[a]] == false) {
                System.out.print(numbers[a]);
                dup[numbers[a]] = true;
            }
        }

If you want to use 2 for loops (which is slower), then you need to only check for the duplicate value before this current index (the for with b should be for (int b = 0; b < a; b++) )

Upvotes: -3

Related Questions