473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

print out the duplicate of an array only once

i want to print out an element of an array only once even if the element has many duplicates. her is the code i have tried;-

Expand|Select|Wrap|Line Numbers
  1.      for(int count=0;count<arr.length;count++){
  2.     int counter = 0;
  3.     char num = arr[count];
  4.       for(int count2=0;count2<arr.length;count2++){
  5.      if(num==arr[count2]){
  6.        counter++; }
  7.     } System.out.println(num + "\t" + counter);}
Dec 28 '12 #1
2 4399
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code.

That code in no way accounts for duplicates. One method is to remove all occurrences of an element after it has been printed. Another method is to insert what has been printed into another array and check against that array before printing. A third method is to sort the array and then print at each change of value.

Which method you use will depend on your requirements. For example, I would use the second method if I needed to keep the original data intact and in its original order.
Dec 28 '12 #2
Anas Mosaad
185 128KB
The fastest and easiest way is to insert all the data into a set. Sets allows no duplicates.
Jan 5 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Robert | last post by:
Im a beginner in PHP and Im having a problem with this code. Im trying to remove duplicate elements from an array created via $_GET. I want users to be able to click on a link which sends an email...
1
by: Stephen | last post by:
I have two arrays: int array1 = { { 1, 2, 3 }, { 4, 5, 6 } }; int array2 = { { 1, 2 }, { 3 }, { 4, 5, 6 } }; I need to print both of them by column like this:
4
by: Shailesh | last post by:
Hi! I want to overload << operator so that it can print an arry defined in MyClass.My problem is that I want to print only a no of elements NOT all the elements in the array and this no of...
0
by: Alan Roberts | last post by:
In VB6 I have a routine that accepts a 2D array and prints it to the debug windows. The array can be of any type (eg integer, string, double etc).... Public Sub DPArray(ArrayToPrint As Variant)...
1
by: undbund | last post by:
Hi, there are lots of examples that show you how to print array values given a key. Do you know how to print keys for a given array. example $myArray = array('a'=>1, 'b'=>2, 'c'=>3, 'd'=>4); ...
10
by: Krustov | last post by:
$rambo="daffy duck"; $rambo="mad max"; $rambo="daffy duck"; $rambo="superman"; etc etc How do i remove duplicate strings from a array ? . ('daffy duck' could appear more than twice in the...
45
by: hank | last post by:
I have this code here, it converts decimal numbers to binary. There is one problem. The output is printed 'in reverse' and I have no clue at all how to solve this problem. Output of program: ...
1
by: ranjitha55 | last post by:
but it is displaying al values der in xml file.it should avoid reading duplicate values ,pls help me.. my code is, public class nm { public static void main(String argv) { try { File...
5
rahulephp
by: rahulephp | last post by:
Hi I need to print below array in the following manner. Output would be: Property Name: 1 Property Address: 1 Price: 1 Property Size: 1 URL 1
0
by: ghjk | last post by:
I'm using jsp. I want to print string array values . How can I do that? please help me. This is my code. ArrayList settings = (ArrayList)session.getAttribute("settingList");
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.