Printing array elements without repeating an element

I have a java class that includes an array of String and two for loops

for passing the elements of the array and their printing, as well as their redundancy in

since they are strings.

I want someone to help me print each element (String) once, even if it

repeated in an array.

The following code prints an element in an array more than once.

So, comparing the elements of a Needed array

public class myClassName { static String [] myArray = {"Khaled","Valderama","Daoud","Khaled","Rasheed","Daoud","Valderama","Khaled"}; public static String [] getArray() { String str[] = new String[myArray.length]; for(int i=0;i<myArray.length;i++) { str[i] = myArray[i].toString(); } return str; } public static void main( String [] args) { String d [] = getArray(); int noOftimesRepeated; for(int i=0;i<getArray().length;i++) { noOftimesRepeated=1; String currentName = d[i]; for(int j=0;j<getArray().length;j++) { if(i!=j && d[i].equalsIgnoreCase(d[j])) { noOftimesRepeated = noOftimesRepeated+1; } } int j =0; System.out.println(d[i]+"\t" +"\t"+noOftimesRepeated); } } 

}

Please have a solution without using .util. * package

I have a second test, but it prints one element and its redundancy

only.

  public class Javafool { static String [] myArray = {"Khaled","Valderama","Daoud","Khaled","Rasheed","Daoud","Valderama","Khalo","Valderama"}; static String str2[] = new String[myArray.length]; public static String [] getArray() { String str[] = new String[myArray.length]; for(int i=0;i<myArray.length;i++) { str[i] = myArray[i].toString(); } return str; } public static void main(String[] args) { String d [] = getArray(); int noOftimesRepeated; sort(myArray); int no_of_repeat=1; String temp =null; int i ; for( i = 0;i<myArray.length-1;i++) { temp = myArray[i]; myArray[i] = myArray[i+1]; myArray[i+1] = temp; if(myArray[i].equals(temp)) { no_of_repeat= ++no_of_repeat; } } System.out.println(myArray[i]+""+temp+"\t"+"\t\t"+no_of_repeat); } public static void sort(String [] array) { String temp = null; for(int j=0;j<array.length;j++) { for(int i = 0; i<array.length-1;i++) { if(array[i].compareTo(array[i+1])<0) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; } }}}} 
+4
source share
11 answers

I found a solution, (The following solution does not include java.util

and it depends on the Quicksort algorithm).

Thanks to everyone.

  public class Javafool { static String [] myArray = {"Khaled","Valderama","Daoud","Khaled","Rasheed","Daoud","Valderama","Khalo","Valderama","Daoud"}; static String str2[] = new String[myArray.length]; public static void main(String[] args) { int [] noOftimesRepeated; sort(myArray); int no_of_repeat=1; String temp =null; int i ; int count = 0; String previous = null; for (String s : myArray) { if (s.equals(previous)) { count++; } else { if( previous !=null) System.out.println(previous + " :" + count); previous = s; count = 1; } } if (myArray.length > 0) { System.out.println(previous + " :" + count); } } public static void sort(String [] array) { String temp = null; for(int j=0;j<array.length;j++) { for(int i = 0; i<array.length-1;i++) { if(array[i].compareTo(array[i+1])<0) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; } } } } } 
0
source

Add lines to Set<String> that eliminate duplicate values ​​and then print them:

 List<String> list = Arrays.asList("Khaled", "Valderama",...); Set<String> set = new LinkedHashSet<String>(list); for(String s : set) System.out.println(s); 
+3
source

Use Map<String, Integer> , String for the input string, Integer for the noOftimesRepeated counter.

Example:

 Map<String , Integer> map = new HashMap<String , Integer>(); // Add and count str Repeated times. map.put(str, map.get(str) + 1); // output string and counter pair in map System.out.println(map); 
+2
source

If you absolutely do not want to use java.util , you can manually sort and delete neighboring duplicates:

 public static void main(String[] args) { String [] myArray = {"Khaled","Valderama","Daoud","Khaled","Rasheed","Daoud","Valderama","Khaled"}; sort(myArray); String last=null; for(int i = 0;i<myArray.length;i++) { if(last==null || !myArray[i].equals(last)) { last = myArray[i]; System.out.print(last+", "); } } } /* * Very naive method to sort elements. You can improve this with a merge sort. * Arrays.sort() would do the same job in a better way. */ public static void sort(String [] array) { String temp = null; for(int j=0;j<array.length;j++) { for(int i = 0; i<array.length-1;i++) { if(array[i].compareTo(array[i+1])<0) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; } } } } 
+1
source
 public static void main(String[] args) { List<String> myArray = Arrays.asList("Khaled","Valderama","Daoud","Khaled","Rasheed","Daoud","Valderama","Khaled"); Set<String> sets = new HashSet<String>(); sets.addAll(myArray); System.out.println(sets); } 

Exit: [Khaled, Valderama, Rasheed, Daoud]

0
source

You can do as below

 String[] myArray = { "Khaled", "Valderama", "Daoud", "Khaled", "Rasheed", "Daoud", "Valderama", "Khaled" }; Set<String> sets = new HashSet<String>(Arrays.asList(myArray)); System.out.println(Arrays.toString(sets.toArray())); 
0
source

You can use set . set will avoid adding duplicates.

  String [] myArray = {"Khaled","Valderama","Daoud", "Khaled","Rasheed","Daoud","Valderama","Khaled"}; Set<String> set=new HashSet<>(); for(String i:myArray){ set.add(i); } System.out.println(set); 

If you do not want to use the java.util.* Package, try the following path.

  String [] myArray = {"Khaled","Valderama","Daoud","Khaled","Rasheed","Daoud", "Valderama","Khaled"}; String[] newArr=new String[myArray.length]; int j=0; for(String i:myArray){ if(!Arrays.toString(newArr).contains(i)){ newArr[j]=i; j++; } } 
0
source

Besides using Set, you can also create a list of unique elements.

 String [] myArray = {"Khaled", "Valderama", "Daoud", "Khaled", "Rasheed", "Daoud", "Valderama", "Khaled"}; List<String> myPrintList = new ArrayList<String>(); for(String str : myArray){ if(!myPrintList.contains(str)){ // Check first if myPrintList contains the item already myPrintList.add(str); // Add if the list doesn't contain that item } } // Print list for(String str : myPrintList){ System.out.println(str); } 

EDIT based on comment:

You don’t know why you do not want to use the util package, but -

 String [] myArray = {"Khaled", "Valderama", "Daoud", "Khaled", "Rasheed", "Daoud", "Valderama", "Khaled"}; StringBuilder uniqueNames = new StringBuilder(); // For storing all unique names separated by a pipe (|) for(String str : myArray){ if(uniqueNames.indexOf(str) < 0){ // Check if str exists in builder yet uniqueNames.append(str); // Add str if it doesn't exist uniqueNames.append("|"); // Add delimiter } } String[] myPrintArray = uniqueNames.toString().split("\\|"); // Get an array by splitting using the pipe delimiter for(String str : myPrintArray){ System.out.println(str); } 
0
source

You need 2 for the loop to do this. Only this 3 lines of code will do !: D

 final List<String> lst = Arrays.asList("Khaled","Valderama","Daoud","Khaled","Rasheed","Daoud","Valderama","Khaled"); final Set<String> set = new HashSet<String>(lst); System.out.printf("Unique values: ", set); 

If without * ulit package

You will need your own sorting method. Pseudocode may look like this (inefficient way to do)

 "Given" lst array; Array temp = new Araay(lst.lenght); //two for loop for (int i = 0; i< lst.lenght; i++) { if(i==0){ temp[i] = lst[i]; // first array } for (int u = 0 ; u < lst.lenght; u ++){ //Write a code here if the lst[i] string is not equal to any temp[u] string, add then inside. Else dun care :) Cheers! } } 
0
source

try it. For Loop will not execute completely for repeating elements.

 import java.util.ArrayList; public class myClassName { static String [] myArray = {"Khaled","Valderama","Daoud","Khaled","Rasheed","Daoud","Valderama","Khaled"}; public static String [] getArray() { String str[] = new String[myArray.length]; for(int i=0;i<myArray.length;i++) { str[i] = myArray[i].toString(); } return str; } public static void main( String [] args) { String d [] = getArray(); int noOftimesRepeated; ArrayList<String> list = new ArrayList<String>(); for(int i=0;i<getArray().length;i++) { if(list.contains(d[i])) continue; noOftimesRepeated=1; for(int j=0;j<getArray().length;j++) { if(i!=j && d[i].equalsIgnoreCase(d[j]) ) { noOftimesRepeated = noOftimesRepeated+1; list.add(d[i]); } } System.out.println(d[i]+"\t" +"\t"+noOftimesRepeated); } } } 
0
source

I agree with the previous guy, firstly, you must make you an arraist in sequence, you can try the Quicksort algorithm, and secondly, you must compare the current element with the previous one, if they are equal, do not print it. it's easy, right?

you can extract your quicksort algorithm function to the util class so you can use it in a later version.

0
source

Source: https://habr.com/ru/post/1501289/


All Articles