Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:42:14+00:00 2026-06-17T11:42:14+00:00

Given a vector of ones and zeros, I would like to count the number

  • 0

Given a vector of ones and zeros, I would like to count the number of entries with a value one. However, the vector may be very long and I only care to know if the vector has zero, one, or more entries with a value o f one.

Using the approach given here, I can count the number of ones in the vector.

(count (filter #{1} [1 0 1 0 0 1 1]))

Can I limit filter (or use some other approach) to avoid visiting any more than three elements of the vector, in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:42:15+00:00Added an answer on June 17, 2026 at 11:42 am

    Filter is lazy, so will only do as much work as required. Since you only care about having no 1’s, one 1’s or two or more ones, you only need to examine up to two elements of the filtered sequence of 1’s, so just take 2 before you count:

    user=> (count (take 2 (filter #{1} [1 0 1 0 0 1 1])))
    2
    user=> (count (take 2 (filter #{1} [0 0 0 0 0 0 0])))
    0
    user=> (count (take 2 (filter #{1} [0 0 0 0 0 0 1])))
    1
    user=> (def rare (repeatedly #(if (< (rand) 0.0001) 1 0)))
    #'user/rare
    user=> (take 10 rare)
    (0 0 0 0 0 0 0 0 0 0)
    user=> (count (take 2 (filter #{1} rare)))
    2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to populate an 2 dimensional array, from a vector. I think
I'd like to reorder columns in my data.table x , given a character vector
For a given vector like this, Vector<Temp> s = new Vector<Temp>(); s.add(new Temp(a,1)); s.add(new
I have a function that processes a given vector, but may also create such
Given a sorted vector with a number of values, as in the following example:
Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
I have to do the following: Given a vector with random numbers, separate it
In R, given a vector casp6 <- c(0.9478638, 0.7477657, 0.9742675, 0.9008372, 0.4873001, 0.5097587, 0.6476510,
Given an STL vector, output only the duplicates in sorted order, e.g., INPUT :
Given: class example { public: std::vector<std::vector<int>> a; int b; } func() { example e;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.