How can I count and print duplicate lines in an array of strings in Java?

I have a dilemma on my hands. After much trial and error, I still could not understand this simple task.

I have one array

String [] array = {anps, anps, anps, bbo, ehllo}; 

I need to be able to go through an array and find duplicates and print them on one line. Words without duplicates should be displayed separately

The output should be like this:

 anps anps anps bbo ehllo 

I tried while, for loops, but logic seems impossible.

+4
source share
7 answers

Sort the array first and then

 for(int i = 0, i < array.length; i++){ String temp = array[i]; System.out.print(temp+" "); for(int j = i+1; j < array.length; j++){ String temp2 = array[j]; if(temp.compareTo(temp2) == 0){ System.out.print(temp2+" "); i++; } } System.out.println(); } 

or something similar...

+2
source

Well, there are alarming numbers of incorrect answers or answers that use a HashMap or HashSet for this very simple iterative problem, so this is the right solution.

 Arrays.sort(array); for (int i = 0; i < array.length; ++i){ if (i+1 == array.length) { System.out.println(array[i]); } else if (array[i].equals(array[i+1])) { System.out.print(array[i]+" "); } else { System.out.println(array[i]); } } 
+9
source

There are several ways to achieve this.

  • Use two for loops, one of which goes through the array and selects the value and the other inner loop, in which you go through the array (from the current index), looking for that value.
  • You may have a map containing words, you scroll the array and fill the map with the number of occurrences corresponding to the value that is currently selected from the array

The second way is better. The code looks something like this:

 Map<String, Integer> occurences = new HashMap<String, Integer>(); for(int index=0; index < array.length; index++){ int nOcc = 1; if(occurences.containsKey(array[index]){ nOcc = occurences.get(array[index]) + 1; } occurences.remove(array[index]); occurences.put(array[index], nOcc); } 

At this point, the map should contain all the words (keys) and their corresponding number of occurrences (values)

+2
source

If you sort the array first, then you can simply check if the current index is equal to the next index (given that you should consider IndexOutOfBounds ) if they are equal, do System.out.print() if they are not equal to a System.Out.println() .

 String [] array = {"anps", "anps", "anps", "bbo", "ehllo"}; // If you already are assured that the strings in the array are sorted // then the sort is not necessary. Arrays.sort(array); for(int i = 0; i < array.length; i++){ if((i+1)==array.length || !array[i].equals(array[(i+1)])){ System.out.println(array[i]); } else { System.out.print(array[i]+" "); } } 
+1
source

The complexity is n^2 , just start from the first value and go to the end, finding the same, if you find the print on one line and go to a new line, you must also delete all the printed value.

The complexity is nlogn + n == nlogn , merge or quick sort, and then go to the end and assign sequence values. There are more solutions, but I think that is enough for you.

0
source

Naive algorithms

  • Create map
  • Iterate over the entire array
  • check if a key exists on the map
  • if yes update +1
  • if there is no insert
  • print the card as you want

You should be able to do what you are looking for!

0
source

Use below logic

 import java.util.ArrayList; public class RepeatStringPrint { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { String[] x = { "anps", "anps", "anps", "bbo", "ehllo" }; String total[] = new String[50]; String sTotal[] = null; for (int i = 0; i < x.length; i++) { total[i] = x[i]; } for (int k = 0; k < total.length; k++) { int count = 0; if (total[k] != null) { sTotal = new String[50]; for (int i = 0; i < total.length; i++) { if (total[k] == total[i]) { count++; if (count <= 1) { sTotal[i] = total[k]; } } } if (sTotal[k] != null) { for(int j=0; j<count; j++){ System.out.print(sTotal[k]+"\t"); } System.out.print("\n"); } } } } catch (Exception e) { } } } 
0
source

All Articles