Can you please tell me the logic for printing each array elements only once without repetition? ( Use C Programming) 🙌🏻🥺 | SoloLearn: Learn to code for FREE!

+9

Can you please tell me the logic for printing each array elements only once without repetition? ( Use C Programming) 🙌🏻🥺

Input1(Array Size): 6 Input2 (Array Elements): 1 1 2 3 3 4 Output: 1 2 3 4 And also count those many numbers in output! Kindly go through my code and let me know your views: https://code.sololearn.com/cQVl5xxu2a84/?ref=app

1/23/2021 7:13:44 AM

Mahima Rajvir Singh

22 Answers

New Answer

+6

Hello Benjamin Jürgens & Rishav ! I have applied a new logic here! Kindly go through the code and let me know if it's correct. See code - https://code.sololearn.com/cQVl5xxu2a84/?ref=app

+8

Hmm , I will just tell you the way - [You have to make it by your own] First take your inputs ,then make a outer loop , "i" ,which runs 0 to 9 and make a inner loop "j" which call each element and inside j check that i == j or not if condition is true then use printf() and print, i and the use break , by this one number will prints only one time. Try it, if you get stuck then tell me I will make code! All the best!

+7

Benjamin Jürgens Thanks buddy to justify! Fixed! Mahima Rajvir Singh ,see my code now it also work on negative numbers :)

+6

I don't know about c programming but thought java u can go with VARARGS: VARIABLE LENGTH ARGUMENT Syntex is Sum(int...a) and by using array Sum(int[] a) just see on YouTube related to this VARARGS LEN... c programming u will get it

+4

Mahima Rajvir Singh Okay , do one thing make a function which find highest element from array Run outer loop i from 0 to largest element of array Runner inner loop for fetch all elements of array and make condition if (i == array[j]) { printf(i); break; } See Correct Code - https://code.sololearn.com/cyILI93l8Ufr/?ref=app All the best!

+4

Rishav - Thanks for explaining! ✨😊 Benjamin Jürgens - Thank you! 🙌🏻 Benjamin Jürgens & Rishav, please check my edited code now in the above asked question. And let me know if it is correct! 😊 And why it is ignoring negative elements? Please find me a solution! 🥺

+4

Benjamin Jürgens & Rishav Thanks for the help! Happy coding! ✨✨🌟

+3

Rishav Please check my code in the above, I have edited my question! Tell me where I am wrong.

+3

Vasile Eftodii Please go through my question, I have clearly explained & showed the output I wanted in the description. Thanks for your code. Learnt new stuffs from it! 😊

+2

It becomes apparent that the task description isn't precise. Should the output be sorted like in Rishav 's solution? Also I think that solution isn't good practice. It works only for int because in line 30 index variable i is compared to content of array

+2

It is ignoring negative elements because i starts at 0 and goes up, so if(i == array[j]) can never be true. It is another flaw of the idea from Rishav. Please take it only as objective criticism. Revert your code to previous version and try my approach. I tested but didn't save. If you get stuck, I can recreate my solution, but first you should try. I think I just changed about 3 lines from your first code. I'm sure you can fix it.

+2

Your code works perfect! That was what i meant. I did the inner loop a bit different: for(j=0;j<i;j++) It results in a different order because it prints at the first occurrence, while yours prints on the last. insignificant i think, since output isn't ordered anyway

+2

Mahima Singh, I am sorry, maybe I didn't understand your task correctly, but I consider that your program is not correct. I wrote another solution. https://code.sololearn.com/cMY9OPJNB31V/?ref=app

+2

Mahima Singh, if I get it correctly, you are trying to compress the array - print consecutively repeating elements only once. That's something else and it means you didn't explain the task correctly from the start! 😑

+1

In your inner loop you want to check if you have seen same value array[i] _before_. So j only goes up to i. If you find the value in previous elements, you don't print and continue with next i. If you didn't find it, it is the first occurrence and you can print it.

+1

Vasile Eftodii Yes, your code is exactly correct for printing the unique numbers from the array while skipping the repeated ones. Here, I wanted to print every element of array only one time. In your code: Input(size): 6 Input(data): 1 1 2 2 3 4 Output: 4 But I was looking for something like this: Input(size): 6 Input(data): 1 1 2 2 3 4 Output: 1 2 3 4 Printing each element of array only once! Hope you got it! 😊 Happy coding! 💯

+1

// Created by Mahima Rajvir Singh // Input 1: Array size // Input 2: Array elements (Include repeating elements) // Output: Each array elements printed only once // Example: // 6 // 2 3 2 1 1 4 // 3 2 1 4 #include<stdio.h> int main() { int array[100],size,i,j,count=0; // Taking input in array: printf("Enter size of array: "); scanf("%d",&size); printf ("\nEnter elements: "); for(i=0;i<size;i++) { scanf("%d",&array[i]); } // Printing each element only once: printf("\nActual array elements: "); for(i=0;i<size;i++) { count=0; for(j=i+1;j<size;j++) { if(array[i]==array[j]) count++; } if(count==0) printf("%d ",array[i]); } return 0; }

+1

this would be more efficient, since we just need to check if its the first time we're seing that number. https://code.sololearn.com/c70VTrOQ11Um/?ref=app

+1

0

This is program in my profile watch