0
 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.

asked Sep 10, 2020 at 10:17
2
  • you're going way to far. create a list of integers. iterate over the array: if list doesn't contain integer: add integer to list and print, if not, go to next iteration Commented Sep 10, 2020 at 10:21
  • @deHaar the value 2 only appears once - he wants to print out the values that appear multiple times but only print that value once. Commented Sep 10, 2020 at 10:59

4 Answers 4

4

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 + " ");
 }
}
answered Sep 10, 2020 at 10:25
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't account for values that only appear once (e.g. 2 in the OP's array). They will be emitted but it has no duplicate.
True, was not entirely sure what OP wanted, but now that functionality is added to my answer.
0

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 + " ");
 }
 }

}

answered Sep 10, 2020 at 10:25

Comments

0

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);
 }
 }
}
answered Sep 10, 2020 at 11:16

Comments

-3

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++) )

answered Sep 10, 2020 at 10:21

13 Comments

why a boolean array? this will stop working as soon as you have two 1001 elements in there. Just use a list that stores unique elements in the array, and use contains
no, this will stop working when the biggest element is 1001 or bigger, as the dup array uses the elements from numbers, and not their indexes
yes, that's what I said. that's a limitation that shouldn't be there.
I think he should just solve this without using any data structures, so that's why I suggested using a frequency array, of course the size can be adjusted to the limitations of the problem
yet another reason not to use an array. which was basically my original point
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.