Java Program to print only the unique elements from the String array without using Collections

7:41 PM 2 Comments A+ a-

In Java, you'll rarely be writing a program without using the arrays. As the arrays put no limitations on what type of elements you can put in them or do not provide any means to check for the duplicates, there might be some scenarios wherein you might need only the distinct elements from the array instead of all its elements.
Suppose we have an array of Strings and we need only the distinct strings out from that array. Obviously, the same task can be done conveniently using Collections API but there might be cases where you'll be just better off not requiring to put any overhead of using the Collections classes such as Sets in this case. Or if this does not convince you, knowing how to do this without using the Collections will still be worth learning if you're going to appear in some interviews or for some coding competitions. As I have repeatedly mentioned in my posts that Java programming questions on Strings are among the hot favorite for interviewers, so I'll be posting many more such String based java programming questions in my future posts.

Java Program to find only distinct elements of String Array without using Collections


public class CodeInJava {

 public static void main(String[] args) {
  //Input array
  String str[] = {"Java","Struts2","Java","Spring3","C++","Python","Java"};
  
  //Creating new array to store only the unique elements
  String[] uniqueArray = new String[str.length];
  
  //copying the first element directly to uniqueArray
  uniqueArray[0] = str[0];
  
  //Counter to keep track of the index to put next value at in uniqueArray
  int counter = 1;
  
  boolean flag = false;
  for(int i = 1;i < str.length;i++) {
   flag = false;
   for (int j = 0;j < i;j++) {
    if(uniqueArray[j] == null)
     j=i;
    //comparing in case-insensitive manner.
    //In case you need to find unique in case sensitive manner, use equals() only
    else if( str[i].equalsIgnoreCase( uniqueArray[j] ) )
     flag = true;
   }
   if( !flag ) {
    uniqueArray[counter] = str[i];
    counter++;
   }
  }
  
  //Printing the unique elements. Counter stores the number of unique elements in the original array.
  for (int i = 0; i < counter; i++) 
   System.out.println(uniqueArray[i]);

 }

}

Currently working at Symantec in Chennai. Previously worked as a Senior Software Engineer at IGATE. Loves to explore new things, loves to code and solve challenging problems with the help of it. Like any other normal person, loves to watch movies, listen to songs (specially when travelling, it is must), getting to know people, travelling and whatever makes me feel good!

2 comments

Write comments
March 11, 2016 at 5:31 PM delete

The programming was very easily understand and more important coding are provided on this post and this is very valuable in my studies,all coding easily understand and develop more skills,thanks for sharing this post.
Oracle SQL Training in Chennai

Reply
avatar
oraclehcm
AUTHOR
March 18, 2017 at 3:30 PM delete

I found your blog very interesting, Thanks a lot for using the great services in this blog.
Our on line coaching are often accessed from anyplace at any time with security access. we provide low price of fee for on-line coaching.
Oracle fusion financials training

Reply
avatar