Printing elements of an array without repeating an element

find unique elements in array java
find sum of non-repeating (distinct) elements in an array
given a distinct array of elements find and non repeating random elements
unique elements in array c++
write a program in c# sharp to print all unique elements in an array
how to get unique values from array in java
non repeating element in array in c
remove duplicates from array java

I have a java class which involves a String array and two for loops

for going through the array elements and prints them plus their redundancy in the

array as they are Strings .

I want someone help me to print each element (String) one time only even it

repeated in the array.

The following code prints some element in the array more than one time.

So comparison between elements of the array Needed

 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 Is there any solution without using .util.* package

I have a second trial but it out prints the one element and it 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;

           }

           }}}}

Add the Strings to Set<String>, which eliminates 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);

Non-Repeating Element, Find the first non-repeating element in a given array of integers. Examples: Input : -1 2 -1 3 2 Output : 3 Explanation : The first number that does� Find the first non-repeating element in a given array of integers. Examples: Input : -1 2 -1 3 2 Output : 3 Explanation : The first number that does not repeat is : 3 Input : 9 4 9 6 7 4 Output : 6

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

Print All Distinct Elements of a given integer array, The given array may contain duplicates and the output should print every element only once. The given array is not sorted. Examples: Input: arr[] =� The first loop will select an element and the second loop will iteration through the array by comparing the selected element with other elements. If a match is found, print the duplicate element. In the above array, the first duplicate will be found at the index 4 which is the duplicate of the element (2) present at index 1.

If you absolutely don't want to use java.util, you can still sort by hand and remove adjacent 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;
      }
    }
  }
}

Print non duplicates in Array, The original title of this video was "remove duplicate values in array". Luckily someone pointed Duration: 13:31 Posted: Mar 26, 2015 Output : Repeating elements are 4 2. Time Complexity: O(n*n) Auxiliary Space: O(1) Method 2 (Use Count array) Traverse the array once. While traversing, keep track of count of all elements in the array using a temp array count[] of size n, when you see an element whose count is already set, print it as duplicate.

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

Output: [Khaled, Valderama, Rasheed, Daoud]

Program to find non repeating element in an array (Unique elements), Algorithm. Declare the array and input the array elements. Start traversing the array and check if the current element is already present in the array. If it is already present in the array, move to the next element in the array and continue. If not, output the element as the non-repeating element. k-th distinct (or non-repeating) element in an array. Queries to check if any non-repeating element exists within range [L, R] of an Array; Find the two non-repeating elements in an array of repeating elements; Find the two repeating elements in a given array; Find sum of non-repeating (distinct) elements in an array

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()));

Program to print the duplicate elements of an array, In this program, we need to print the duplicate elements present in the array. This can be done through two loops. The first loop will select an element and the� C Program to Print Elements in an Array. This program to print an array in c allows the user to enter the Size and the row elements of One Dimensional Array. Next, we are using For Loop to iterate the array values and print all the elements in this array

C program to print the duplicate elements of an array, In this program, we need to print the duplicate elements present in the array. This can be done through two loops. The first loop will select an element and the� For each array element Insert it as key & increase frequencies. For same key it will only increase frequencies. For i=0: n-1 hash[array [i]]++; End For Now to print the repeated character we need to print the keys (array elements) having value (frequencies) more than 1. Set an iterator to hash.begin().

C exercises: Print all unique elements of an array, Write a program in C to print all unique elements in an array. %d elements in the array :\n",n); for(i=0;i<n;i++) { printf("element - %d : ",i); scanf("%d",&arr1[i]); } /* Checking duplicate elements in the array */ printf("\nThe without="" output,="" and="" check="" next="" one="" a[i],="" if="" no="" repeating=""� How to find maximum repeating element of an array in O(n) time and without using any extra memory space. Given an integer array of size N, containing elements from 0 to K where k N. We have to find maximum repeating number in array in O(n) time complexity and O(1) space complexity.

How to remove duplicates from a Python List, print(mylist). Now we have a List without any duplicates, and it has the same order as the original List. Print the List to demonstrate the result� In the above program, since each element in array contains another array, just using Arrays.toString() prints the address of the elements (nested array). To get the numbers from the inner array, we just another function Arrays.deepToString(). This gets us the numbers 1, 2 and so on, we are looking for.

Comments
  • I upvoted both answers. If your need is only to remove duplicates, use arjacsoh's solution. If you need to count occurrences of each String, use lulyon's solution.
  • Please Is there any solution without using .util.* package
  • Is it a requirement to absolutely avoid java.util.* package? I see some solutions without it (manual sorting?) but it is nearly equivalent as the methods exposed in java.util and it will likely be error prone.
  • s it a requirement to absolutely avoid java.util.* package? Yes Exactly.
  • Please Is there any solution without using .util.* package
  • @ElhadiMamoun I have a more manual approach(require more code of course) by sorting the array first and count the neighbored strings. But the time complexity is o(nlogn), better than two nested forloop, which is o(n^2).
  • @Ruchira Thank you. code is not complete here, just an example.
  • @lulyon Is your new manual approach avoids java.util.*; package?
  • @ElhadiMamoun It could be. The first step, sorting, which can be manually implementing with Quicksort algorithm. The second step(counting the neighbored strings by a pass for string array) is itself avoid using java package.
  • Your code is good as not repeating the displaying of elements but it doesn't count the number a of String redundancy
  • You have a sorted array. It is not very difficult to count occurrences of adjacent elements.
  • 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; } I changed little into your code , now the code can prints the number of redundancy for one element only , can you guide me more plz?