LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2015, 09:58 AM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 101

Rep: Reputation: Disabled
Print if number occurs more than once in array


I written program to count the number of times element in array appears.
And if it appears more than once I need to print.
Code is
Code:
#include <iostream>
#include <stdio.h>

using namespace std;
int main()
{
	int arr[6] = {5, 3, 7, 5, 1, 3};
	int count = 0,i,j;
	for(i = 0;i < 6;i++)
	{
		for(j = 0;j<6;j++)
		{
			if(arr[i] == arr[j])
				count++;
		}
		if(count > 1)
		cout << "Element " << arr[i] << " occured " << count << " times " << endl;
		count = 0;
	}
	return 0;
}
output is
Code:
Element 5 occured 2 times 
Element 3 occured 2 times 
Element 5 occured 2 times 
Element 3 occured 2 times
But in this case 5 and 3 are printing as many times as it is present in array.
What condition can be written so duplicates are not printed?
i.e
My expected output is
Code:
Element 5 occured 2 times 
Element 3 occured 2 times

Last edited by srinietrx; 09-22-2015 at 11:37 AM.
 
Old 09-22-2015, 10:51 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
A gentle suggestion: it is helpful for you to identify your language of choice.
You may do this with tags and/or a specific reference in your post.

Daniel B. Martin
 
1 members found this post helpful.
Old 09-22-2015, 12:13 PM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
I think the easiest way to modify your code to do that is to check whether j > i in your inner loop, i.e. if you find a number equal to arr[i], but before it, break the inner loop and continue to the next iteration of the outer one.
Other than that, there's a couple of other options, such as

- sorting the array before counting
- remembering which numbers you already encountered
- using an associative data structure such as a map
 
1 members found this post helpful.
Old 09-22-2015, 12:21 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by srinietrx View Post
... in this case 5 and 3 are printing as many times as it is present in array.
What condition can be written so duplicates are not printed?
Homework?

Your outer loop encounters 5 and 3 twice, hence why it prints those statements twice.

One thing you can consider is to code it such that you remember and store elements which have been detected to be repeated. For each new element processed in your outer loop, check whether or not they appear in your remembered list of repeats. If so, then skip checking any further.
 
Old 09-22-2015, 01:56 PM   #5
srinietrx
Member
 
Registered: May 2013
Posts: 101

Original Poster
Rep: Reputation: Disabled
Thanks
@millgates j > i in innerloop solves the problem
But I will try to implement using map.It is opportunity to learn STL

@rtmistler :- I was thinking about that logic you are saying.Thanks for hint.
 
  


Reply

Tags
c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux commands to print the number of users & number of processes!!? m7com Linux - Newbie 3 04-30-2014 12:59 PM
[SOLVED] PHP: Count number of value in sub array angel115 Programming 2 08-26-2011 03:35 AM
number of element in a array of pointer? os2 Programming 7 03-09-2005 11:50 AM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM
repeated number in a 4x4 array xbaez Programming 3 11-21-2004 11:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration